Search code examples
iphoneobjective-ccocoa-touchrespondstoselector

Objective-C: How to check if a C function is supported


How can I perform a run-time check to see if I can use UIGraphicsBeginImageContextWithOptions, which is only available starting with iOS 4.

I know I could check [[UIDevice currentDevice] systemVersion], but Apple recommends using things like NSClassFromString() or respondsToSelector:. Is there a respondsToSelector: for C functions?


Solution

  • Here's another option, which I've been using.

    C functions are pointers. If you "weak" link to UIKit framework, on iOS 3 the function pointer will simply be NULL, so you can test for the existence of the function by doing:

    if (UIGraphicsBeginImageContextWithOptions)
    {
        // On iOS 4+, use the main screen's native scale factor (for iPhone 4).
        UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    }
    else
    {
        UIGraphicsBeginImageContext(size);
    }
    

    See also: How do I weak link frameworks on Xcode 4?