开发者

Using iOS 5 features without breaking backward compatibility

My application is currently compiled against the latest iOS 4.x SDK. Once iOS 5.0 SDK is out, would it be possible for me to use the new iOS 5.0 SDK features in my application and yet have it run on iOS 4 devices (but with the parts using开发者_如何学C the new features disabled)?


To answer the question generally, the Objective-C runtime is fully reflective, which means that you can query which methods an object supports (via respondsToSelector:) and get hold of classes by name at runtime (via NSClassFromString). iOS binaries also support the concept of weak linkage with frameworks, which means that the framework will be loaded if it is available but that you don't consider it a fatal error if the framework isn't available (as is the default behaviour).

That means that when Apple release new versions of the OS you can write code that uses new features on the latest version of the OS but functions fine without them if those new features are new bits of API.

Apple also sometimes supply new SDK features that aren't new APIs, such as when the Clang static analyser was added to Xcode. As those features usually don't require any runtime support they're fully usable. iOS 5 is a little unique because Apple's commits to the LLVM project suggest that there are some new compile time features in amongst the ARC stuff that rely on some runtime support. So they'll be unavailable, if indeed they're in the tools as Apple intend to distribute them.


You can check wether some functions are available or not during runtime.

For example:

Class motionManagerClass = NSClassFromString(@"CMMotionManager");
if(motionManagerClass) {
    CMMotionManager *motionManager = [[CMMotionManager alloc] init];
    if([motionManager isGyroAvailable]) {
        //iOS device with gyro
    }else {
        //right iOS but device has no gyro
    }
}else{
//wrong iOS
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜