Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to weak linking in iPhone SDK?

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

like image 799
Moshe Avatar asked Dec 08 '22 02:12

Moshe


1 Answers

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.

like image 123
Jasarien Avatar answered Dec 30 '22 16:12

Jasarien