Search code examples
objective-cios8backwards-compatibilitytouch-id

How to check if a class or CFType is available in Objective-C


I am working in iOS8 right now and I wanted to check if the CFType 'SecAccessControlRef' is available. Since the app will support iOS 7, without a check, the app will crash. For a regular NSClass I would just use a check like

if (NSClassFromString('SomeClass') == NULL)

but is there something similar for non NSClass items?


Solution

  • Rather than testing for the type itself you can test for one of the functions that support the type. When a framework is weak-linked the address of C functions will be NULL if the framework is not present. For example you should be able to use something like:

    BOOL isSecAccessControlRefAvailable = SecAccessControlCreateWithFlags != NULL;
    

    See Using Weakly Linked Methods, Functions, and Symbols in Apple's SDK Compatibility Guide.