10

Possible Duplicate:
Check iPhone iOS Version

One of the changes made in iOS 5 is the ability to override the drawrect methods. This means I need to change the appearance of the navigationBar and tabBar on a different way. I am able to use apple new methods:

[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"navigationBarBackgroundRetro.png"] forBarMetrics:UIBarMetricsDefault];

//I create my TabBar controlelr
tabBarController = [[UITabBarController alloc] init];

// I create the array that will contain all the view controlers
[[UITabBar appearance] setBackgroundImage:
    [UIImage imageNamed:@"navigationBarBackgroundRetroTab.png"]];

[[UITabBar appearance] setSelectionIndicatorImage:
    [UIImage imageNamed:@"tab_select_indicator"]];

I'm developing an app for iOS 4.3 and 5.0. However, iOS 5 ignores the drawrect method that I'm overriding, so it should run the above code. How can I check the iOS version so I can use the above code if the device is on iOS 5?

Community
  • 1
  • 1
Alex van Rijs
  • 803
  • 5
  • 17
  • 39

1 Answers1

50

The samples below work for any version number. e.g.: to detect iOS 5 instead 7, replace 7 with a 5 in the code.

AvailabilityInternal.h macros

This detects the SDK you are building with:

#ifdef __IPHONE_7_0
  // iOS 7.0
#endif

This detects the version set as Deployment Target in the General tab of your target configuration:

  #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000 
    // iOS 7.0 or later
  #else 
    // less than 7
  #endif

NSFoundation version

BOOL isAtLeastIOS61 = NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_6_1;
BOOL isAtMost61 = NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1;
BOOL is7x = floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1;

If you ⌘ click NSFoundationVersionNumber, you'll see version constants for iOS and OSX. The constant for the current SDK is always missing.

Core Foundation version

BOOL atLeastIOS61 = kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_6_1;

As with NSFoundationVersionNumber, the SDK version is missing.

Device system version

NSString *version = [[UIDevice currentDevice] systemVersion];

BOOL isAtLeast6 = [version floatValue] >= 6.0;
BOOL isAtLeast7 = [version floatValue] >= 7.0;

An alternative way:

BOOL isAtLeast6 = [version hasPrefix:@"6."];
BOOL isAtLeast7 = [version hasPrefix:@"7."];

An alternative way:

BOOL isAtLeast6 = [version compare:@"6.0" options:NSNumericSearch] != NSOrderedAscending
BOOL isAtLeast7 = [version compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending

In case of concerns about float/string conversion, let it be know that everything above reports correctly if the version is equal or greater, for any possible iOS version (6.0, 6.0.1, 6.1, etc.).

Jano
  • 62,815
  • 21
  • 164
  • 192