Alternatives to weak linking in iPhone SDK?
Asked Answered
D

1

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

Dissemblance answered 16/6, 2010 at 18:15 Comment(0)
F
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.

Filiation answered 16/6, 2010 at 18:33 Comment(5)
Can you please explain weak linking here, for completeness' sake?Dissemblance
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.Filiation
How do I weak link? Can you link me a tutorial? I couldn't find something that was helpful.Dissemblance
@Dissemblance - I describe how to do this in my answer here: #2619389 .Plumbaginaceous
Find references to new APIs that didn't exist pre-4? Great answer hereHereditary

© 2022 - 2024 — McMap. All rights reserved.