Alternatives to weak linking in iPhone SDK?

妖精的绣舞 提交于 2019-11-26 22:51:45

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/3056016/alternatives-to-weak-linking-in-iphone-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!