问题
I am trying to create a universal binary that supports multitasking on the iPhone 4 and can still run on the iPad.
I know how to avoid compile errors for different versions of the iPhone IOS by checking if a class exists by using NSClassFromString and "respondToSelector", but is there a way to check for the existence of constants like UIBackgroundTaskInvalid?
I of course can use #IFDEF, but I want to avoid this.
回答1:
You do it as follows:
if (NULL != &UIBackgroundTaskInvalid) {
//do multitasking stuff here
} else {
// don't do multitasking stuff here.
}
Basically you want to verify that the address of that variable exists.
Update: To be clear, you can't really use an #ifdef for this since you will build with a version of the SDK that contains the symbols.
回答2:
It's probably sufficient to test for the existence of a method that you know is associated with the constant.
回答3:
The preferred method to check for multitasking iOS is to see if UIDevice responds to isMultitaskingSupported, like this:
//----------------------------------------------------------------------
// returns YES if multitasking is available (iOS 4.0 or >)
//----------------------------------------------------------------------
BOOL hasMultitasking()
{
UIDevice* device = [UIDevice currentDevice];
if ([device respondsToSelector:@selector(isMultitaskingSupported)]) {
return [device isMultitaskingSupported];
}
return NO;
}
If multitasking is supported then you can use those multitasking related constants, which you should have weak linked to.
来源:https://stackoverflow.com/questions/3362544/how-to-test-for-the-existance-of-a-constant-when-creating-a-iphone-universal-bin