How to dynamically test for available global variables in frameworks?
As my iPhone app can run on both OS 3 and 4, I need a way to safely test for iOS 4 SDK features.
I like to avoid checking the [UIDevice ... systemVersion] string (I wonder why Apple failed to provide a numeric value here for easy testing, as it's available on OS X).
Anyway. The usual clean way to test for SDK features is to check if a class reponds to a selector, like开发者_StackOverflow社区 this:
if ([UIApplication instancesRespondToSelector:...
And for C methods, one simply checks if the function pointer is NULL:
if (newFunction != NULL) ...
But my problem is that I need to check if a global variable exists. E.g. this one:
extern NSString *const UIApplicationDidEnterBackgroundNotification __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);
Any idea how one can test their existence at runtime?
Extracted from the help document "SDK Compatibility guide":
Check the availability of an external (extern) constant or a notification name by explicitly comparing its address—and not the symbol’s bare name—to NULL or nil.
For instance, if you wanted to check for the key UIKeyboardFrameBeginUserInfoKey
in a keyboard notification dictionary, which is only available in 3.2 and later, you could write:
if (&UIKeyboardFrameBeginUserInfoKey) {
blah;
} else {
blah;
}
the default method is
UIDevice* device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)])
backgroundSupported = device.multitaskingSupported;
When the test is passed your "it exists in 4.x" global variable shoudl be available.
By the way - it answers if OS4 is there an if the device supports multitasking. Manfred
精彩评论