Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check function availability in Core Foundation

I want to use a Core Foundation method that is only available on the latest iOS SDK, and fallback to a different one for previous SDKs. Is there something similar to Cocoa's way of doing it for NSObject?:

 - (BOOL)respondsToSelector:(SEL)aSelector

A bit more of context, let's say ABAddressBookCreate() would only be available in iOS5, and my app is targeting iOS 4.3+. I would want to check if ABAddressBookCreate() is available at runtime and perform different actions depending on that.

like image 223
Jan S. Avatar asked Mar 27 '26 23:03

Jan S.


1 Answers

Try this (doesn't need weak linking):

#include <dlfcn.h>

void *handle = dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_NOW);
void *createFunc = dlsym(handle, "ABAddressBookCreate");

if (createFunc != NULL)
{
    // available
    ABAddressBookRef (*_ABAddressBookCreate)(void) = createFunc;
    // then use this function pointer as normally
}

If you can provide weak linking:

if (ABAddressBookCreate != NULL)
{
   // available
}

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!