Mobilewits
Mobilewits

Reputation: 1763

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

Upvotes: 3

Views: 533

Answers (3)

Paul de Lange
Paul de Lange

Reputation: 10633

You can control this setting in the Xcode (4.3) Build Settings tab of your target. Where it talks about "Other Linker Flags" you can add a conditional setting (click and hold the lower right "+" icon that says "Add Build Setting" by default). Here you can specify the library to link using normal -l/-L linker flags but only for builds specified as "Any iOS SDK", but don't add this flag for "Any iOS Simulator SDK".

Upvotes: 0

Richard J. Ross III
Richard J. Ross III

Reputation: 55583

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.

Upvotes: 4

skram
skram

Reputation: 5314

What you can do is build the Static Library as a FAT one. Compiling for both architectures armv and i386 to run in the simulator.

You can find more information about this here,

http://mark.aufflick.com/blog/2010/11/18/making-a-fat-static-library-for-ios-device-and-simulator

Upvotes: 0

Related Questions