Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally link a third party API in Xcode

I have a third party API that I am trying to integrate into my iOS Universal application. The API works fine if ran on a device but throws me a compile time link error when trying to run it on the simulator. So, is there a way I can skip their static library linking if I run on the simulator?

Thanks

like image 859
Mobilewits Avatar asked Dec 29 '25 00:12

Mobilewits


1 Answers

Actually, It's a lot easier than I thought.

Step 1: Add the linker flags -ObjC and -all_load to your target. This tells the objc runtime that even if we don't reference a class in code, it will still load it into memory.

Step 2: In your code, you can do this:

Class cls = NSClassFromString(@"SomeClassInStaticLibrary");
if (cls == nil)
{
   // on the simulator
}
else
{
   // on the device, use the class like usual
   id myInstance = [[cls alloc] init];
}

Unfortunately, you have to refer to everything as an id, because if you include the headers, you WILL get a linker error.

Its a bit of a hack, but it works.

like image 87
Richard J. Ross III Avatar answered Dec 31 '25 13:12

Richard J. Ross III



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!