views:

436

answers:

2

I'm looking to make my app compatible with older versions of iPhone OS. I did see weak linking mentioned as an option. Can I use OS version detection code to avoid code blocks that the OS can't handle? (Say iAD?)

if(OS >= 4.0){
//set up iADs using "NDA code"...
} 

If yes, what goes in place of if(OS >= 4.0)?

A: 

Quite ugly, not good on many levels and utterly hackish, but should work: wrap your "NDA code" inside a try-catch block and do nothing on error...

code_burgar
Not a good solution at all.
Jasarien
@Jasarien hence the "Quite ugly, not good on many levels and utterly hackish" :/
code_burgar
bottom line, will it keep my app running?
Moshe
@Moshe, not reliably. Not every API throws an exception on error. Try/Catch blocks won't save you in every instance of a non existing API.
Jasarien
+5  A: 

You should be weak linking against the new frameworks. Alongside that you should be checking the availability of new APIs using methods like NSClassFromString, respondsToSelector, instancesRespondToSelector etc.

Eg. Weak linking against MessageUI.framework (an old example, but still relevant)

First check if the MFMailComposerController class exists:

Class mailComposerClass = NSClassFromString(@"MFMailComposerController");
if (mailComposerClass != nil)
{
    // class exists, you can use it
}
else
{
    // class doesn't exist, work around for older OS
}

If you need to use new constants, types or functions, you can do something like:

if (&UIApplicationWillEnterBackgroundNotification != nil)
{
    // go ahead and use it
}

If you need to know if you can use anew methods on an already existing class, you can do:

if ([existingInstance respondsToSelector:@selector(someSelector)])
{
    // method exists
}

And so on. Hope this helps.

Jasarien
Can you please explain weak linking here, for completeness' sake?
Moshe
Sure, weak linking allows you to link your code against newer libraries and frameworks so that at runtime the link to the framework isn't enforced strongly (meaning your app won't crash when it can't find the library on an older OS. If you strong link against a framework or library that isn't included on the device when the app runs it will crash with a (quite cryptic) error about not finding the library. Weak linking prevents this.
Jasarien
Strong linking is good for optimization.
Jasarien
How do I weak link? Can you link me a tutorial? I couldn't find something that was helpful.
Moshe
Found one. Excellent.
Moshe
@Moshe - I describe how to do this in my answer here: http://stackoverflow.com/questions/2618889/universal-iphone-ipad-application-debug-compilation-error-for-iphone-testing/2622027#2622027 .
Brad Larson