4

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)?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Moshe
  • 57,511
  • 78
  • 272
  • 425

1 Answers1

15

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
  • 58,279
  • 31
  • 157
  • 188
  • Can you please explain weak linking here, for completeness' sake? – Moshe Jun 16 '10 at 18:35
  • 1
    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 Jun 16 '10 at 18:54
  • How do I weak link? Can you link me a tutorial? I couldn't find something that was helpful. – Moshe Jun 16 '10 at 19:11
  • @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 Jun 16 '10 at 20:57
  • Find references to new APIs that didn't exist pre-4? Great answer [here](http://stackoverflow.com/questions/7790497/iphone-app-developed-with-sdk-4-2-requires-backward-compatibility-with-ios-3-1) – Steve Rogers Nov 25 '11 at 05:25