Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the new features disabled)?

like image 765
sparkymat Avatar asked Sep 05 '11 07:09

sparkymat


2 Answers

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.

like image 60
Tommy Avatar answered Nov 07 '22 05:11

Tommy


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
}
like image 20
jussi Avatar answered Nov 07 '22 05:11

jussi