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 .