Check if device is running iOS 5 or higher
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.).
Alex van Rijs
Updated on January 28, 2020Comments
-
Alex van Rijs about 4 years
Possible Duplicate:
Check iPhone iOS VersionOne 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? -
nschum over 12 yearsThat just checks the SDK used for compilation. It's not a runtime check and won't work as intended.
-
Mark Norgren about 12 yearsThis answer doesn't work if the device is running 4.3.2 It will returns true for the > 4.3 check.
-
Jano about 12 yearsFixed (until iOS 6 at least!). I didn't expect 4.3.3 either when I wrote it. :)