【Topics Generic】Supporting Multiple Versions of iOS

来源:互联网 发布:linux常用软件大全 编辑:程序博客网 时间:2024/04/28 18:38

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 theinstancesRespondToSelector:class method or therespondsToSelector: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 givenClassobject 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 theNSClassFromStringfunction to see whethera class is defined. If the function returns a value other thannil, 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 nametoNULL. If the symbol is notNULL, 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, seeSDKCompatibility Guide


0 0
原创粉丝点击