Checking iOS version
Water
|
04/15/2014
|

1. Compile-time checking OS version

_IPHONEOSVERSIONMAX_ALLOWED

This will check the maximum allowed OS version, that's the Base SDK version that the project is using. So we can include some code that feature is only available in newer SDK. But if we always assume coders are using newest version of SDK, this directive is rarely used.

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_6_0
-(BOOL)shouldAutorotate
{
    return NO;
}
#endif

_IPHONEOSVERSIONMIN_REQUIRED

Check the Deployment Target in your project setting, i.e., minimum OS version that the app is allowed to run. We can use this to exclude some old code that is only needed on older OS version. e.g. if the minimum requirement is OS 6.0, there is no need to add old rotation code to the source.

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return NO;      
}
#endif

2. Run-time checking OS version

NSFoundationVersionNumber

Each OS contains a double global variable NSFoundationVersionNumber that indicates the OS version. The value doesn't actually relate to the OS version but will increase across version update. So we can code to do different code in different OS version quickly.

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
    // iOS 7
}
else {
}

Strange enough, NSFoundationVersionNumber_iOS_7_0 or NSFoundationVersionNumber_iOS_7_1 is not avaialble in the SDK yet...

UIDevice

We can also get the OS version number in string format:

[UIDevice currentDevice].systemVersion

so if the OS version is 3.1.3, it will return @"3.1.3"

respondToSelector

Sometimes, it is more logical to check if the object supports newer APIs. But beware that sometimes some method is already existed in older OS as hidden API with different behavior.

Checking object:

if ([view respondToSelector:@selector(snapshotViewAfterScreenUpdates:)]) {
    // snapshot method exists in iOS 7
}

Checking class:

if ([UIView instancesRespondToSelector:@selector(snapshotViewAfterScreenUpdates:)]) {
    // snapshot method exists in iOS 7
}

3. Checking iPhone or iPad

Finally, a very common code snippet to check if the current device is an iPhone or iPad.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    // on iPad
}
else {
    // on iPhone
}