Search code examples
iosobjective-cbooleanbackwards-compatibility

Detect BOOL availability to support multiple iOS versions?


I am trying to find out how one can detect if an external BOOL is available so that I can support iOS 7 and 8. New in iOS 8 is a BOOL you can use to find out if Reduce Transparency is enabled, and I want to implement that check in an if statement, but this will crash on iOS 7 without first checking if the extern BOOL is available. I was surprised I could not find the answer from my web searches.

Here is the BOOl definition:

UIKIT_EXTERN BOOL UIAccessibilityIsReduceTransparencyEnabled() NS_AVAILABLE_IOS(8_0);

And the location I'm using it:

if (UIAccessibilityIsReduceTransparencyEnabled()) {
    NSLog(@"transparency is disabled");
}

Solution

  • Please read the SDK Compatibility Guide.

    What you need to do is check if the function UIAccessibilityIsReduceTransparencyEnabled exists:

    if (UIAccessibilityIsReduceTransparencyEnabled != NULL) {
        // function exists, use it
        if (UIAccessibilityIsReduceTransparencyEnabled()) {
            NSLog(@"transparency is disabled");
        }
    } else {
        // function doesn't exist, do something else
    }