iPhone SDK中弱链接的替代方法? [英] Alternatives to weak linking in iPhone SDK?
问题描述
我想让我的应用程式与旧版iPhone OS兼容。我确实看到弱链接被提到作为一个选项。我可以使用操作系统版本检测代码来避免操作系统无法处理的代码块吗? (说出iAD?)
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(OS> = 4.0)
?
推荐答案
新框架。此外,您应该使用 NSClassFromString
, responsesToSelector
,等方法检查新API的可用性。 instancesRespondToSelector
等。
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.
例如。与MessageUI.framework的弱连接(一个旧示例,但仍然相关)
Eg. Weak linking against MessageUI.framework (an old example, but still relevant)
首先检查 MFMailComposerController
:
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
}
等等。希望这有助于。
这篇关于iPhone SDK中弱链接的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!