【Topics Generic】Supporting Multiple Versions of iOS

Any app that supports a range of iOS versions must use runtime checks to prevent the use of newer APIs onolder versions of iOS that do not support them. For example, if you build your app using new features in iOS6 but your app still supports iOS 5, runtime checks allow you to use recently introduced features when theyare available and to follow alternate code paths when they are not. Failure to include such checks will causeyour app to crash when it tries to use new symbols that are not available on the older operating system.

There are several types of checks that you can make:

To determine whether a method is available on an existing class, use the instancesRespondToSelector:class method or the respondsToSelector: instance method.

Apps that link against iOS SDK 4.2 and later can use the weak linking support introduced in that versionof the SDK. This support lets you check for the existence of a given Class object to determine whetheryou can use that class. For example:

if ([UIPrintInteractionController class]) {
   // Create an instance of the class and use it.

}else {

   // The print interaction controller is not available.
}

To use this feature, you must build your app using LLVM and Clang and the app’s deployment target mustbe set to iOS 3.1 or later.

Apps that link against iOS SDK 4.1 and earlier must use the NSClassFromString function to see whethera class is defined. If the function returns a value other than nil, you may use the class. For example:

Class splitVCClass = NSClassFromString(@"UISplitViewController");
if (splitVCClass)
{
   UISplitViewController* mySplitViewController = [[splitVCClass alloc]
init];
   // Configure the split view controller.
}

To determine whether a C-based function is available, perform a Boolean comparison of the function nameto NULL. If the symbol is not NULL, you can use the function. For example: 

if (UIGraphicsBeginPDFPage != NULL)
{
    UIGraphicsBeginPDFPage();
}

For more information and examples of how to write code that supports multiple deployment targets, see SDKCompatibility Guide


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值