Search code examples
iphonemacos

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 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?


Solution

  • 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;
    }