How to check iOS version?
Asked Answered
M

36

909

I want to check if the iOS version of the device is greater than 3.1.3 I tried things like:

[[UIDevice currentDevice].systemVersion floatValue]

but it does not work, I just want a:

if (version > 3.1.3) { }

How can I achieve this?

Mabel answered 26/7, 2010 at 23:30 Comment(2)
Moderator Note: Over time, this question has accrued over 30 answers. Before adding a new answer, be sure that your solution has not already been provided.Driver
Starting Xcode 7, if #available(iOS 9, *) {} in Swift. Starting Xcode 9, if (@available(iOS 11, *)) {} in objective-c.Fein
A
1051

The quick answer …


As of Swift 2.0, you can use #available in an if or guard to protect code that should only be run on certain systems.

if #available(iOS 9, *) {}


In Objective-C, you need to check the system version and perform a comparison.

[[NSProcessInfo processInfo] operatingSystemVersion] in iOS 8 and above.

As of Xcode 9:

if (@available(iOS 9, *)) {}


The full answer …

In Objective-C, and Swift in rare cases, it's better to avoid relying on the operating system version as an indication of device or OS capabilities. There is usually a more reliable method of checking whether a particular feature or class is available.

Checking for the presence of APIs:

For example, you can check if UIPopoverController is available on the current device using NSClassFromString:

if (NSClassFromString(@"UIPopoverController")) {
    // Do something
}

For weakly linked classes, it is safe to message the class, directly. Notably, this works for frameworks that aren't explicitly linked as "Required". For missing classes, the expression evaluates to nil, failing the condition:

if ([LAContext class]) {
    // Do something
}

Some classes, like CLLocationManager and UIDevice, provide methods to check device capabilities:

if ([CLLocationManager headingAvailable]) {
    // Do something
}

Checking for the presence of symbols:

Very occasionally, you must check for the presence of a constant. This came up in iOS 8 with the introduction of UIApplicationOpenSettingsURLString, used to load Settings app via -openURL:. The value didn't exist prior to iOS 8. Passing nil to this API will crash, so you must take care to verify the existence of the constant first:

if (&UIApplicationOpenSettingsURLString != NULL) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}

Comparing against the operating system version:

Let's assume you're faced with the relatively rare need to check the operating system version. For projects targeting iOS 8 and above, NSProcessInfo includes a method for performing version comparisons with less chance of error:

- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version

Projects targeting older systems can use systemVersion on UIDevice. Apple uses it in their GLSprite sample code.

// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
// class is used as fallback when it isn't available.
NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
    displayLinkSupported = TRUE;
}

If for whatever reason you decide that systemVersion is what you want, make sure to treat it as an string or you risk truncating the patch revision number (eg. 3.1.2 -> 3.1).

Alcoholometer answered 26/7, 2010 at 23:44 Comment(11)
There are specific cases where checking for system version is warranted. For example, a couple of classes and methods that were private n 3.x were made public in 4.0, so if you simply checked for their availability you would get a wrong result in 3.x. Additionally, the way that UIScrollViews handle zoom scales changed subtly in 3.2 and above, so you would need to check OS versions in order to process the results appropriately.Tempi
iOS 3.2 does not appear to send -viewWillAppear in a UISplitViewController. My hack is to determine if < iOS 4.0, and send it to the Detail View Controller myself in the Root View's -didSelectRowAtIndexPath.Beamon
You need to be a bit careful here because [@"10.0" compare:@"10" options:NSNumericSearch] returns NSOrderedDescending, which might well not be intended at all. (I might expect NSOrderedSame.) This is at least a theoretical possibility.Haily
Yep, I just encountered a case where a iOS 5 bug needed to be circumvented. respondsToSelector et al wouldn't do the job -- have to compare versions.Twitter
I don't see the reason for not relying on the version string supplied by UIDevice. A better way would be to rely on the version string first, then still check respondsToSelector to verify the call wont crash the app.Gomulka
Funny thing with iOS8 is, when you want to use isOperatingSystemAtLeastVersion, you first have to check with respondsToSelector, and if it does it's iOS8 anyway.:)Urquhart
The [[UIDevice currentDevice] systemVersion] method is pretty expensive compared to @CarlJ's answer that's also suggested by Apple for iOS versions smaller 8, so I'd rather use NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_x_y if you have to support older OS versions.Spotlight
If you just want the version string, there's a method operatingSystemVersionString since iOS 2.0.Persinger
To append to @BradLarson's comment, checking for version number became much more important in UI classes when 7.0 came out, changing nearly everything about the UI. Suddenly, custom controls with iOS6-like styles were ugly, so we needed to check the OS version and choose which styles/assets to use in iOS7+Original
With the introduction of TVos this has even become more important. See as TVos will return 9.0 while the 9.1 SDK is used. So check the class or method(selector) is the best way, after this you should check the NSFoundationVersionNumber and only if that is not possible should you check the system versions on UIDevice.Salmons
Does this if #available(iOS 11, *) check work for all versions later than iOS 11? Like iOS 12? Or just all versions where major number is 11?Hakon
S
1061
/*
 *  System Versioning Preprocessor Macros
 */ 

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

/*
 *  Usage
 */ 

if (SYSTEM_VERSION_LESS_THAN(@"4.0")) {
    ...
}

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"3.1.1")) {
    ...
}
Shaun answered 26/7, 2010 at 23:30 Comment(8)
+1. Keeping this in a header that you can include where needed is the easiest solution I can think of. In several cases this is more reliable than checking for availability using NSClassFromString. Another example is in iAd, where if you use ADBannerContentSizeIdentifierPortrait in a version before 4.2 you'll get EXEC_BAD_ACCESS, but the equivalent ADBannerContentSizeIdentifier320x50 is deprecated after 4.1.Plenary
Another place I use this is with UIPageViewController and setting the style to UIPageViewControllerTransitionStyleScroll in iOS6.Rollicking
This is perfect, should be the accepted answer. I would just add that for methods only for version greater than the base version should still be verified using respondsToSelector.Gomulka
This doesn't work for 10s. For example comparing 4.10 and 4.9 would give the wrong result.Turgot
One should be very careful when using optional .0 numbers. For example SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0.0") gives incorrect result on iOS 7.0.Gaylene
I just tested putting this in my .pch file and it works great (building with Xcode 5 at least)Alfy
CarlJ's answer below suggested by Apple is betterFarley
Thanks everyone for appreciating you use it in your projects under BSDShaun
A
1051

The quick answer …


As of Swift 2.0, you can use #available in an if or guard to protect code that should only be run on certain systems.

if #available(iOS 9, *) {}


In Objective-C, you need to check the system version and perform a comparison.

[[NSProcessInfo processInfo] operatingSystemVersion] in iOS 8 and above.

As of Xcode 9:

if (@available(iOS 9, *)) {}


The full answer …

In Objective-C, and Swift in rare cases, it's better to avoid relying on the operating system version as an indication of device or OS capabilities. There is usually a more reliable method of checking whether a particular feature or class is available.

Checking for the presence of APIs:

For example, you can check if UIPopoverController is available on the current device using NSClassFromString:

if (NSClassFromString(@"UIPopoverController")) {
    // Do something
}

For weakly linked classes, it is safe to message the class, directly. Notably, this works for frameworks that aren't explicitly linked as "Required". For missing classes, the expression evaluates to nil, failing the condition:

if ([LAContext class]) {
    // Do something
}

Some classes, like CLLocationManager and UIDevice, provide methods to check device capabilities:

if ([CLLocationManager headingAvailable]) {
    // Do something
}

Checking for the presence of symbols:

Very occasionally, you must check for the presence of a constant. This came up in iOS 8 with the introduction of UIApplicationOpenSettingsURLString, used to load Settings app via -openURL:. The value didn't exist prior to iOS 8. Passing nil to this API will crash, so you must take care to verify the existence of the constant first:

if (&UIApplicationOpenSettingsURLString != NULL) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}

Comparing against the operating system version:

Let's assume you're faced with the relatively rare need to check the operating system version. For projects targeting iOS 8 and above, NSProcessInfo includes a method for performing version comparisons with less chance of error:

- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version

Projects targeting older systems can use systemVersion on UIDevice. Apple uses it in their GLSprite sample code.

// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
// class is used as fallback when it isn't available.
NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
    displayLinkSupported = TRUE;
}

If for whatever reason you decide that systemVersion is what you want, make sure to treat it as an string or you risk truncating the patch revision number (eg. 3.1.2 -> 3.1).

Alcoholometer answered 26/7, 2010 at 23:44 Comment(11)
There are specific cases where checking for system version is warranted. For example, a couple of classes and methods that were private n 3.x were made public in 4.0, so if you simply checked for their availability you would get a wrong result in 3.x. Additionally, the way that UIScrollViews handle zoom scales changed subtly in 3.2 and above, so you would need to check OS versions in order to process the results appropriately.Tempi
iOS 3.2 does not appear to send -viewWillAppear in a UISplitViewController. My hack is to determine if < iOS 4.0, and send it to the Detail View Controller myself in the Root View's -didSelectRowAtIndexPath.Beamon
You need to be a bit careful here because [@"10.0" compare:@"10" options:NSNumericSearch] returns NSOrderedDescending, which might well not be intended at all. (I might expect NSOrderedSame.) This is at least a theoretical possibility.Haily
Yep, I just encountered a case where a iOS 5 bug needed to be circumvented. respondsToSelector et al wouldn't do the job -- have to compare versions.Twitter
I don't see the reason for not relying on the version string supplied by UIDevice. A better way would be to rely on the version string first, then still check respondsToSelector to verify the call wont crash the app.Gomulka
Funny thing with iOS8 is, when you want to use isOperatingSystemAtLeastVersion, you first have to check with respondsToSelector, and if it does it's iOS8 anyway.:)Urquhart
The [[UIDevice currentDevice] systemVersion] method is pretty expensive compared to @CarlJ's answer that's also suggested by Apple for iOS versions smaller 8, so I'd rather use NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_x_y if you have to support older OS versions.Spotlight
If you just want the version string, there's a method operatingSystemVersionString since iOS 2.0.Persinger
To append to @BradLarson's comment, checking for version number became much more important in UI classes when 7.0 came out, changing nearly everything about the UI. Suddenly, custom controls with iOS6-like styles were ugly, so we needed to check the OS version and choose which styles/assets to use in iOS7+Original
With the introduction of TVos this has even become more important. See as TVos will return 9.0 while the 9.1 SDK is used. So check the class or method(selector) is the best way, after this you should check the NSFoundationVersionNumber and only if that is not possible should you check the system versions on UIDevice.Salmons
Does this if #available(iOS 11, *) check work for all versions later than iOS 11? Like iOS 12? Or just all versions where major number is 11?Hakon
T
252

As suggested by the official Apple docs: you can use the NSFoundationVersionNumber, from the NSObjCRuntime.h header file.

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    // here you go with iOS 7
}
Tilda answered 6/8, 2013 at 9:39 Comment(17)
+1 This is in fact the safest and most accurate option here.Marrakech
Um..NSFoundationVersionNumber_iOS_6_1 does not exist in iOS 5 SDK.Devan
@Nate I use iOS 7 SKD myself. What I did now is to check whether NSFoundationVersionNumber_iOS_6_1 defined or not, if not, of course it's below iOS 7. I need to consider this case cause I need to make sure the lib is suit for every developers, including ones still use iOS 5 SDK. Any way, this is a good solution, I agree with it. That's why I use it. ;)Devan
@Devan you can define the missing versionnumbers by your self: github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h (take a look at line 102-130)Tilda
@Nate yes you're right. But I just want to fix the layouts' frame, like status bar's offset. :) You can take a look at THIS DEMO, that's what I did now. Feel free to give any suggestion for it! Thanks!!Devan
@Devan i would recommend to define the missing versionsnumbers by yourself and always check the current version with a if(...) condition, not with a #ifdef conditionTilda
@Cœur than you can simply check: if(NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_7_0) { ... }Tilda
no, you're wrong, NSFoundationVersionNumber_iOS_7_0 doesn't exist. But well, we could define it with #ifndef NSFoundationVersionNumber_iOS_7_0 #define NSFoundationVersionNumber_iOS_7_0 1047.00 #endif. To avoid messing like that, I went with the SYSTEM_VERSION_LESS_THAN(v) macro from yasirmturkFein
This is definitely the right approach and should be the top answer, considering Apple recommends it. If you want to learn more, see this post about why it's the right way.Suetonius
@JoshBrown thank for the post; i edited the post and added the floor check + the link to the official apple docu.Tilda
I'm on SDK iOS8 now but there is no NSFoundationVersionNumber_iOS_8_0 for iOS8? Did Apple just forget to put it in or is there a different approach on iOS8 now?Banquette
nope, but apple will add the NSFoundationVersionNumber_iOS_8_0 in iOS 9. just check for NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1Tilda
I guess this is the fastest method, better than comparing system stringsMehala
nope, NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_7_0Technology
It is fairly common for two iOS versions to be given the same Foundation version number. Recent examples: 9.2 & 9.3 are both 1242.12, 8.3 & 8.4 are both 1144.17. Another reason to prefer yasirmturk's answer.Fuld
If you want to check is it iOS 10, can use > NSFoundationVersionNumber_iOS_9_x_MaxOdeen
This seems not be helpful for checking any iOS version higher than iOS 9.4Yolk
K
141

Starting Xcode 9, in Objective-C:

if (@available(iOS 11, *)) {
    // iOS 11 (or newer) ObjC code
} else {
    // iOS 10 or older code
}

Starting Xcode 7, in Swift:

if #available(iOS 11, *) {
    // iOS 11 (or newer) Swift code
} else {
    // iOS 10 or older code
}

For the version, you can specify the MAJOR, the MINOR or the PATCH (see http://semver.org/ for definitions). Examples:

  • iOS 11 and iOS 11.0 are the same minimal version
  • iOS 10, iOS 10.3, iOS 10.3.1 are different minimal versions

You can input values for any of those systems:

  • iOS, macOS, watchOS, tvOS, visionOS, ... (see documentation)

Real case example taken from one of my pods:

if #available(iOS 10.0, tvOS 10.0, *) {
    // iOS 10+ and tvOS 10+ Swift code
} else {
    // iOS 9 and tvOS 9 older code
}

Documentation of available

Kellum answered 8/6, 2017 at 7:33 Comment(10)
How can this be inverted in either Objective-C or Swift?Fiedling
@Fiedling if (...) {} else { ... }Fein
Can I use guard in Objective-C instead of leaving the block open and indent in else statement?Fiedling
@Fiedling no, just use an empty if block followed by an else.Fein
That's the solution I currently have, just wanted to avoid unnecessary indentation and empty if blocks.Fiedling
Does the above if statement work, if the OS is something 11.1.3? Or is it just checking for 11.0?Rainwater
does the if (@available(iOS 11, *)) do a runtime check for iOS 11 version or a compile time check for the iOS 11 SDK? .. or both?Afterburner
@Afterburner Essentially a runtime check. But compiler will use it as a hint for warnings.Fein
Does this if #available(iOS 11, *) check work for iOS 12? Or only iOS 11 versions?Hakon
@bkSwifty #available(iOS 11, *) works for iOS 11 and anything newer (11.1, 11.2, ..., 12.0, 12.1, ...), hence the comment: "iOS 11 (or newer)"Fein
W
39

Try:

NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"3.1.3" options: NSNumericSearch];
if (order == NSOrderedSame || order == NSOrderedDescending) {
    // OS version >= 3.1.3
} else {
    // OS version < 3.1.3
}
Wellinformed answered 26/7, 2010 at 23:33 Comment(4)
Why not reverse the order here and save a comparision, since both @"3.1.3" and systemVersion are NSStrings? Ie: ` if ([@"3.1.3" compare: [UIDevice currentDevice].systemVersion options: NSNumericSearch] == NSOrderedDescending) ;// OS version >= 3.1.3 else ;// OS version < 3.1.3`Cupped
Doing that may make the logic less clear. The operation should be equivalent, however.Wellinformed
A comparison between two integers is surely not very expensive.Gynarchy
It's really a matter of style. The overhead of message dispatch will vastly outweigh the extra integer comparison.Wellinformed
K
35

Preferred Approach

In Swift 2.0 Apple added availability checking using a far more convenient syntax (Read more here). Now you can check the OS version with a cleaner syntax:

if #available(iOS 9, *) {
    // Then we are on iOS 9
} else {
    // iOS 8 or earlier
}

This is the preferred over checking respondsToSelector etc (What's New In Swift). Now the compiler will always warn you if you aren't guarding your code properly.


Pre Swift 2.0

New in iOS 8 is NSProcessInfo allowing for better semantic versioning checks.

Deploying on iOS 8 and greater

For minimum deployment targets of iOS 8.0 or above, use NSProcessInfo operatingSystemVersion or isOperatingSystemAtLeastVersion.

This would yield the following:

let minimumVersion = NSOperatingSystemVersion(majorVersion: 8, minorVersion: 1, patchVersion: 2)
if NSProcessInfo().isOperatingSystemAtLeastVersion(minimumVersion) {
    //current version is >= (8.1.2)
} else {
    //current version is < (8.1.2)
}

Deploying on iOS 7

For minimum deployment targets of iOS 7.1 or below, use compare with NSStringCompareOptions.NumericSearch on UIDevice systemVersion.

This would yield:

let minimumVersionString = "3.1.3"
let versionComparison = UIDevice.currentDevice().systemVersion.compare(minimumVersionString, options: .NumericSearch)
switch versionComparison {
    case .OrderedSame, .OrderedDescending:
        //current version is >= (3.1.3)
        break
    case .OrderedAscending:
        //current version is < (3.1.3)
        fallthrough
    default:
        break;
}

More reading at NSHipster.

Kania answered 7/10, 2014 at 11:33 Comment(0)
H
9

I always keep those in my Constants.h file:

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES) 
#define IS_OS_5_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
#define IS_OS_6_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
#define IS_OS_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
#define IS_OS_8_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
Helga answered 15/3, 2015 at 10:10 Comment(2)
Great. Better to place this in .pch file of your XCode ProjectChichihaerh
I place all my constants in my Constants.h file that is imported in my pch file.Helga
H
6
+(BOOL)doesSystemVersionMeetRequirement:(NSString *)minRequirement{

// eg  NSString *reqSysVer = @"4.0";


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

  if ([currSysVer compare:minRequirement options:NSNumericSearch] != NSOrderedAscending)
  {
    return YES;
  }else{
    return NO;
  }


}
Histiocyte answered 20/11, 2012 at 12:26 Comment(1)
@Histiocyte Periods are not decimal points in versioning and are relevant. Your answer does not ignore the periods, It would fail if it did. In versioning each period separated element is a full number. Ex: '1.23.45' is greater than '1.2.345' since second element '23' is greater than '2'. If you would eliminate periods it would be the same number.Rowlandson
H
6

With Version class that is contained in nv-ios-version project (Apache License, Version 2.0), it is easy to get and compare iOS version. An example code below dumps the iOS version and checks whether the version is greater than or equal to 6.0.

// Get the system version of iOS at runtime.
NSString *versionString = [[UIDevice currentDevice] systemVersion];

// Convert the version string to a Version instance.
Version *version = [Version versionWithString:versionString];

// Dump the major, minor and micro version numbers.
NSLog(@"version = [%d, %d, %d]",
    version.major, version.minor, version.micro);

// Check whether the version is greater than or equal to 6.0.
if ([version isGreaterThanOrEqualToMajor:6 minor:0])
{
    // The iOS version is greater than or equal to 6.0.
}

// Another way to check whether iOS version is
// greater than or equal to 6.0.
if (6 <= version.major)
{
    // The iOS version is greater than or equal to 6.0.
}

Project Page: nv-ios-version
TakahikoKawasaki/nv-ios-version

Blog: Get and compare iOS version at runtime with Version class
Get and compare iOS version at runtime with Version class

Haydon answered 3/4, 2013 at 10:30 Comment(0)
R
6

I know this is an old question, but someone should have mentioned the compile-time macros in Availability.h. All of the other methods here are runtime solutions, and will not work in a header file, class category, or ivar definition.

For these situations, use

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0 && defined(__IPHONE_14_0)
  // iOS 14+ code here
#else
  // Pre iOS 14 code here
#endif

h/t this answer

Righteous answered 18/9, 2014 at 3:19 Comment(0)
S
4
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        // Your code here
}

Where of course, NSFoundationVersionNumber_iOS_6_1 must be changed to by applicable for the iOS version you want to check. What I have now written will probably be used a lot when testing if a device is running iOS7 or a previous version.

Sinistrad answered 4/10, 2013 at 11:43 Comment(0)
L
4

New way to check the system version using the swift Forget [[UIDevice currentDevice] systemVersion] and NSFoundationVersionNumber.

We can use NSProcessInfo -isOperatingSystemAtLeastVersion

     import Foundation

     let yosemite = NSOperatingSystemVersion(majorVersion: 10, minorVersion: 10, patchVersion: 0)
     NSProcessInfo().isOperatingSystemAtLeastVersion(yosemite) // false
Levitan answered 11/6, 2014 at 9:2 Comment(0)
O
4

a bit late to the party but in light of iOS 8.0 out there this might be relevant:

if you can avoid using

[[UIDevice currentDevice] systemVersion]

Instead check for existence of of a method/class/whatever else.

if ([self.yourClassInstance respondsToSelector:@selector(<yourMethod>)]) 
{ 
    //do stuff 
}

I found it to be useful for location manager where I have to call requestWhenInUseAuthorization for iOS 8.0 but the method is not available for iOS < 8

Osteoid answered 23/9, 2014 at 23:46 Comment(0)
P
4

UIDevice+IOSVersion.h

@interface UIDevice (IOSVersion)

+ (BOOL)isCurrentIOSVersionEqualToVersion:(NSString *)iOSVersion;
+ (BOOL)isCurrentIOSVersionGreaterThanVersion:(NSString *)iOSVersion;
+ (BOOL)isCurrentIOSVersionGreaterThanOrEqualToVersion:(NSString *)iOSVersion;
+ (BOOL)isCurrentIOSVersionLessThanVersion:(NSString *)iOSVersion;
+ (BOOL)isCurrentIOSVersionLessThanOrEqualToVersion:(NSString *)iOSVersion

@end

UIDevice+IOSVersion.m

#import "UIDevice+IOSVersion.h"

@implementation UIDevice (IOSVersion)

+ (BOOL)isCurrentIOSVersionEqualToVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] == NSOrderedSame;
}

+ (BOOL)isCurrentIOSVersionGreaterThanVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] == NSOrderedDescending;
}

+ (BOOL)isCurrentIOSVersionGreaterThanOrEqualToVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] != NSOrderedAscending;
}

+ (BOOL)isCurrentIOSVersionLessThanVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] == NSOrderedAscending;
}

+ (BOOL)isCurrentIOSVersionLessThanOrEqualToVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] != NSOrderedDescending;
}

@end
Philps answered 7/8, 2015 at 9:31 Comment(0)
L
4

Just for retrieving the OS version string value:

[[UIDevice currentDevice] systemVersion]
Lavonda answered 10/8, 2015 at 7:49 Comment(0)
H
3

In general it's better to ask if an object can perform a given selector, rather than checking a version number to decide if it must be present.

When this is not an option, you do need to be a bit careful here because [@"5.0" compare:@"5" options:NSNumericSearch] returns NSOrderedDescending which might well not be intended at all; I might expect NSOrderedSame here. This is at least a theoretical concern, one that is worth defending against in my opinion.

Also worth considering is the possibility of a bad version input which can not reasonably be compared to. Apple supplies the three predefined constants NSOrderedAscending, NSOrderedSame and NSOrderedDescending but I can think of a use for some thing called NSOrderedUnordered in the event I can't compare two things and I want to return a value indicating this.

What's more, it's not impossible that Apple will some day expand their three predefined constants to allow a variety of return values, making a comparison != NSOrderedAscending unwise.

With this said, consider the following code.

typedef enum {kSKOrderedNotOrdered = -2, kSKOrderedAscending = -1, kSKOrderedSame = 0, kSKOrderedDescending = 1} SKComparisonResult;

@interface SKComparator : NSObject
+ (SKComparisonResult)comparePointSeparatedVersionNumber:(NSString *)vOne withPointSeparatedVersionNumber:(NSString *)vTwo;
@end

@implementation SKComparator
+ (SKComparisonResult)comparePointSeparatedVersionNumber:(NSString *)vOne withPointSeparatedVersionNumber:(NSString *)vTwo {
  if (!vOne || !vTwo || [vOne length] < 1 || [vTwo length] < 1 || [vOne rangeOfString:@".."].location != NSNotFound ||
    [vTwo rangeOfString:@".."].location != NSNotFound) {
    return SKOrderedNotOrdered;
  }
  NSCharacterSet *numericalCharSet = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];
  NSString *vOneTrimmed = [vOne stringByTrimmingCharactersInSet:numericalCharSet];
  NSString *vTwoTrimmed = [vTwo stringByTrimmingCharactersInSet:numericalCharSet];
  if ([vOneTrimmed length] > 0 || [vTwoTrimmed length] > 0) {
    return SKOrderedNotOrdered;
  }
  NSArray *vOneArray = [vOne componentsSeparatedByString:@"."];
  NSArray *vTwoArray = [vTwo componentsSeparatedByString:@"."];
  for (NSUInteger i = 0; i < MIN([vOneArray count], [vTwoArray count]); i++) {
    NSInteger vOneInt = [[vOneArray objectAtIndex:i] intValue];
    NSInteger vTwoInt = [[vTwoArray objectAtIndex:i] intValue];
    if (vOneInt > vTwoInt) {
      return kSKOrderedDescending;
    } else if (vOneInt < vTwoInt) {
      return kSKOrderedAscending;
    }
  }
  if ([vOneArray count] > [vTwoArray count]) {
    for (NSUInteger i = [vTwoArray count]; i < [vOneArray count]; i++) {
      if ([[vOneArray objectAtIndex:i] intValue] > 0) {
        return kSKOrderedDescending;
      }
    }
  } else if ([vOneArray count] < [vTwoArray count]) {
    for (NSUInteger i = [vOneArray count]; i < [vTwoArray count]; i++) {
      if ([[vTwoArray objectAtIndex:i] intValue] > 0) {
        return kSKOrderedAscending;
      }
    }
  }
  return kSKOrderedSame;
}
@end
Haily answered 10/12, 2011 at 12:7 Comment(0)
E
3
#define _kisiOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

if (_kisiOS7) {
            NSLog(@"iOS7 or greater")
} 
else {
           NSLog(@"Less than iOS7");
}
Edmonson answered 17/5, 2014 at 11:19 Comment(0)
U
3
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

Then add a if condition as follows:-

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
   //Your code
}       
Unobtrusive answered 8/2, 2017 at 14:15 Comment(0)
U
2

There are version like 7.0 or 6.0.3, so we can simply convert version into numerics to compare. if version is like 7.0, simply append another ".0" to it and then take its numeric value.

 int version;
 NSString* iosVersion=[[UIDevice currentDevice] systemVersion];
 NSArray* components=[iosVersion componentsSeparatedByString:@"."];
 if ([components count]==2) {
    iosVersion=[NSString stringWithFormat:@"%@.0",iosVersion];

 }
 iosVersion=[iosVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
 version=[iosVersion integerValue];

For 6.0.0

  if (version==600) {
    // Do something
  }

for 7.0

 if (version==700) {
   // Do something
 }
Ultranationalism answered 11/11, 2013 at 10:5 Comment(2)
simply removing the decimal points is the wrong approach. consider a component that is more than 1 character long, e.g. 6.1.10. then this algorithm will yield 6110 > 700, which isn't correct.Arguelles
I understand apple is bad at rolling out new versions but in most of time version's second number never went beyond 5, so 10 is out of question. This is an adhoc approach so if it handles case for 2 components you can modify it to handle four case aswel if you want to do manually. Anyhow this approach was commented at time of iOS6 or 7. iOS 9 and 10 have much better ways of checking it i-e #available.Ultranationalism
C
2

Try the below code:

NSString *versionString = [[UIDevice currentDevice] systemVersion];
Cirrus answered 23/3, 2014 at 10:10 Comment(0)
U
1

As a variation of yasimturks solution, I defined one function and a few enum values instead of five macros. I find it more elegant, but that's a matter of taste.

Usage:

if (systemVersion(LessThan, @"5.0")) ...

.h file:

typedef enum {
  LessThan,
  LessOrEqual,
  Equal,
  GreaterOrEqual,
  GreaterThan,
  NotEqual
} Comparison;

BOOL systemVersion(Comparison test, NSString* version);

.m file:

BOOL systemVersion(Comparison test, NSString* version) {
  NSComparisonResult result = [[[UIDevice currentDevice] systemVersion] compare: version options: NSNumericSearch];
  switch (test) {
    case LessThan:       return result == NSOrderedAscending;
    case LessOrEqual:    return result != NSOrderedDescending;
    case Equal:          return result == NSOrderedSame;
    case GreaterOrEqual: return result != NSOrderedAscending;
    case GreaterThan:    return result == NSOrderedDescending;
    case NotEqual:       return result != NSOrderedSame;
  }
}

You should add your app's prefix to the names, especially to the Comparison type.

Ucayali answered 7/10, 2013 at 17:47 Comment(0)
E
1

Using the refered recommended way... if there is no definition in the header files, you can always get the versión printing it on console with a device of the desired IOS versión.

- (BOOL) isIOS8OrAbove{
    float version802 = 1140.109985;
    float version8= 1139.100000; // there is no def like NSFoundationVersionNumber_iOS_7_1 for ios 8 yet?
    NSLog(@"la version actual es [%f]", NSFoundationVersionNumber);
    if (NSFoundationVersionNumber >= version8){
        return true;
    }
    return false;
}
Erythema answered 2/10, 2014 at 15:59 Comment(0)
H
1

Solution for checking iOS version in Swift

switch (UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch)) {
    case .OrderedAscending:
       println("iOS < 8.0")

    case .OrderedSame, .OrderedDescending:
       println("iOS >= 8.0")
}

Con of this solution: it is simply bad practice to check against OS version numbers, whichever way you do it. One should never hard code dependencies in this way, always check for features, capabilities or the existence of a class. Consider this; Apple may release a backwards compatible version of a class, if they did then the code you suggest would never use it as your logic looks for an OS version number and NOT the existence of the class.

(Source of this information)

Solution for checking the class existence in Swift

if (objc_getClass("UIAlertController") == nil) {
   // iOS 7
} else {
   // iOS 8+
}

Do not use if (NSClassFromString("UIAlertController") == nil) because it works correctly on the iOS simulator using iOS 7.1 and 8.2, but if you test on a real device using iOS 7.1, you will unfortunately notice that you will never pass through the else part of the code snippet.

Honaker answered 19/3, 2015 at 16:33 Comment(1)
Why down voting? This solution works perfectly using Swift and any iOS version. Especially the checking of the class existence is just perfect.Honaker
T
1
#define IsIOS8 (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
Talyah answered 30/5, 2015 at 9:48 Comment(0)
W
0

A more generic version in Obj-C++ 11 (you could probably replace some of this stuff with the NSString/C functions, but this is less verbose. This gives you two mechanisms. splitSystemVersion gives you an array of all the parts which is useful if you just want to switch on the major version (e.g. switch([self splitSystemVersion][0]) {case 4: break; case 5: break; }).

#include <boost/lexical_cast.hpp>

- (std::vector<int>) splitSystemVersion {
    std::string version = [[[UIDevice currentDevice] systemVersion] UTF8String];
    std::vector<int> versions;
    auto i = version.begin();

    while (i != version.end()) {
        auto nextIllegalChar = std::find_if(i, version.end(), [] (char c) -> bool { return !isdigit(c); } );
        std::string versionPart(i, nextIllegalChar);
        i = std::find_if(nextIllegalChar, version.end(), isdigit);

        versions.push_back(boost::lexical_cast<int>(versionPart));
    }

    return versions;
}

/** Losslessly parse system version into a number
 * @return <0>: the version as a number,
 * @return <1>: how many numeric parts went into the composed number. e.g.
 * X.Y.Z = 3.  You need this to know how to compare again <0>
 */
- (std::tuple<int, int>) parseSystemVersion {
    std::string version = [[[UIDevice currentDevice] systemVersion] UTF8String];
    int versionAsNumber = 0;
    int nParts = 0;

    auto i = version.begin();
    while (i != version.end()) {
        auto nextIllegalChar = std::find_if(i, version.end(), [] (char c) -> bool { return !isdigit(c); } );
        std::string versionPart(i, nextIllegalChar);
        i = std::find_if(nextIllegalChar, version.end(), isdigit);

        int part = (boost::lexical_cast<int>(versionPart));
        versionAsNumber = versionAsNumber * 100 + part;
        nParts ++;
    }

    return {versionAsNumber, nParts};
}


/** Assume that the system version will not go beyond X.Y.Z.W format.
 * @return The version string.
 */
- (int) parseSystemVersionAlt {
    std::string version = [[[UIDevice currentDevice] systemVersion] UTF8String];
    int versionAsNumber = 0;
    int nParts = 0;

    auto i = version.begin();
    while (i != version.end() && nParts < 4) {
        auto nextIllegalChar = std::find_if(i, version.end(), [] (char c) -> bool { return !isdigit(c); } );
        std::string versionPart(i, nextIllegalChar);
        i = std::find_if(nextIllegalChar, version.end(), isdigit);

        int part = (boost::lexical_cast<int>(versionPart));
        versionAsNumber = versionAsNumber * 100 + part;
        nParts ++;
    }

    // don't forget to pad as systemVersion may have less parts (i.e. X.Y).
    for (; nParts < 4; nParts++) {
        versionAsNumber *= 100;
    }

    return versionAsNumber;
}
Woodward answered 30/11, 2012 at 3:58 Comment(0)
S
0

Try this

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { 
// do some work
}
Slue answered 3/4, 2014 at 13:32 Comment(2)
Explain what this does.Secretariat
if will check whether the os version is ios 7 or notSlue
S
0
float deviceOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
float versionToBeCompared = 3.1.3; //(For Example in your case)

if(deviceOSVersion < versionToBeCompared)
   //Do whatever you need to do. Device version is lesser than 3.1.3(in your case)
else 
   //Device version should be either equal to the version you specified or above
Scalable answered 12/11, 2014 at 10:7 Comment(1)
float versionToBeCompared = 3.1.3; can't even compile.Polyneuritis
M
0

Swift example that actually works:

switch UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch) {
case .OrderedSame, .OrderedDescending:
    println("iOS >= 8.0")
case .OrderedAscending:
    println("iOS < 8.0")
}

Don't use NSProcessInfo cause it doesn't work under 8.0, so its pretty much useless until 2016

Machellemachete answered 8/12, 2014 at 13:29 Comment(0)
F
0

Here is a swift version:

struct iOSVersion {
    static let SYS_VERSION_FLOAT = (UIDevice.currentDevice().systemVersion as NSString).floatValue
    static let iOS7 = (Version.SYS_VERSION_FLOAT < 8.0 && Version.SYS_VERSION_FLOAT >= 7.0)
    static let iOS8 = (Version.SYS_VERSION_FLOAT >= 8.0 && Version.SYS_VERSION_FLOAT < 9.0)
    static let iOS9 = (Version.SYS_VERSION_FLOAT >= 9.0 && Version.SYS_VERSION_FLOAT < 10.0)
}

Usage:

if iOSVersion.iOS8 {
    //Do iOS8 code here
}
Frontality answered 23/7, 2015 at 12:11 Comment(1)
I think Version should be iOSVersion in line 3-6 like struct iOSVersion { static let SYS_VERSION_FLOAT = (UIDevice.currentDevice().systemVersion as NSString).floatValue static let iOS7 = (iOSVersion.SYS_VERSION_FLOAT < 8.0 && iOSVersion.SYS_VERSION_FLOAT >= 7.0) static let iOS8 = (iOSVersion.SYS_VERSION_FLOAT >= 8.0 && iOSVersion.SYS_VERSION_FLOAT < 9.0) static let iOS9 = (iOSVersion.SYS_VERSION_FLOAT >= 9.0 && iOSVersion.SYS_VERSION_FLOAT < 10.0) }Reba
R
0

Here is a Swift version of yasirmturk macros. Hope it helps some peoples

// MARK: System versionning

func SYSTEM_VERSION_EQUAL_TO(v: String) -> Bool {
    return UIDevice.currentDevice().systemVersion.compare(v, options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedSame
}

func SYSTEM_VERSION_GREATER_THAN(v: String) -> Bool {
    return UIDevice.currentDevice().systemVersion.compare(v, options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedDescending
}

func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v: String) -> Bool {
    return UIDevice.currentDevice().systemVersion.compare(v, options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedAscending
}

func SYSTEM_VERSION_LESS_THAN(v: String) -> Bool {
    return UIDevice.currentDevice().systemVersion.compare(v, options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedAscending
}

func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v: String) -> Bool {
    return UIDevice.currentDevice().systemVersion.compare(v, options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedDescending
}

let kIsIOS7: Bool = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO("7")
let kIsIOS7_1: Bool = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO("7.1")
let kIsIOS8: Bool = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO("8")
let kIsIOS9: Bool = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO("9")
Resigned answered 20/1, 2016 at 0:28 Comment(0)
C
0

Basically the same idea as this one https://mcmap.net/q/23741/-how-to-check-ios-version but more robust:

#ifndef func_i_system_version_field
#define func_i_system_version_field

inline static int i_system_version_field(unsigned int fieldIndex) {
  NSString* const versionString = UIDevice.currentDevice.systemVersion;
  NSArray<NSString*>* const versionFields = [versionString componentsSeparatedByString:@"."];
  if (fieldIndex < versionFields.count) {
    NSString* const field = versionFields[fieldIndex];
    return field.intValue;
  }
  NSLog(@"[WARNING] i_system_version(%iu): field index not present in version string '%@'.", fieldIndex, versionString);
  return -1; // error indicator
}

#endif

Simply place the above code in a header file.

Usage:

int major = i_system_version_field(0);
int minor = i_system_version_field(1);
int patch = i_system_version_field(2);
Counterfoil answered 31/1, 2020 at 10:13 Comment(0)
C
-1

My solution is add a utility method to your utilities class (hint hint) to parse the system version and manually compensate for float number ordering.

Also, this code is rather simple, so I hope it helps some newbies. Simply pass in a target float, and get back a BOOL.

Declare it in your shared class like this:

(+) (BOOL) iOSMeetsOrExceedsVersion:(float)targetVersion;

Call it like this:

BOOL shouldBranch = [SharedClass iOSMeetsOrExceedsVersion:5.0101];

(+) (BOOL) iOSMeetsOrExceedsVersion:(float)targetVersion {

/*
 Note: the incoming targetVersion should use 2 digits for each subVersion --

 example 5.01 for v5.1, 5.11 for v5.11 (aka subversions above 9), 5.0101 for v5.1.1, etc.
*/

// Logic: as a string, system version may have more than 2 segments (example: 5.1.1)
// so, a direct conversion to a float may return an invalid number
// instead, parse each part directly

NSArray *sysVersion = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
float floatVersion = [[sysVersion objectAtIndex:0] floatValue];
if (sysVersion.count > 1) {
    NSString* subVersion = [sysVersion objectAtIndex:1];
    if (subVersion.length == 1)
        floatVersion += ([[sysVersion objectAtIndex:1] floatValue] *0.01);
    else
        floatVersion += ([[sysVersion objectAtIndex:1] floatValue] *0.10);
}
if (sysVersion.count > 2) {
    NSString* subVersion = [sysVersion objectAtIndex:2];
    if (subVersion.length == 1)
        floatVersion += ([[sysVersion objectAtIndex:2] floatValue] *0.0001);
    else
        floatVersion += ([[sysVersion objectAtIndex:2] floatValue] *0.0010);
}

if (floatVersion  >= targetVersion) 
    return TRUE;

// else
return FALSE;
 }
Cordiecordier answered 10/1, 2013 at 17:31 Comment(0)
A
-1

There are a few problems with the two popular answers:

  1. Comparing strings using NSNumericSearch sometimes has unintuitive results (the SYSTEM_VERSION_* macros all suffer from this):

    [@"10.0" compare:@"10" options:NSNumericSearch] // returns NSOrderedDescending instead of NSOrderedSame
    

    FIX: Normalize your strings first and then perform the comparisons. could be annoying trying to get both strings in identical formats.

  2. Using the foundation framework version symbols is not possible when checking future releases

    NSFoundationVersionNumber_iOS_6_1 // does not exist in iOS 5 SDK
    

    FIX: Perform two separate tests to ensure the symbol exists AND THEN compare symbols. However another here:

  3. The foundation framwork versions symbols are not unique to iOS versions. Multiple iOS releases can have the same framework version.

    9.2 & 9.3 are both 1242.12
    8.3 & 8.4 are both 1144.17
    

    FIX: I believe this issue is unresolvable


To resolve these issues, the following method treats version number strings as base-10000 numbers (each major/minor/patch component is an individual digit) and performs a base conversion to decimal for easy comparison using integer operators.

Two other methods were added for conveniently comparing iOS version strings and for comparing strings with arbitrary number of components.

+ (SInt64)integerFromVersionString:(NSString *)versionString withComponentCount:(NSUInteger)componentCount
{
    //
    // performs base conversion from a version string to a decimal value. the version string is interpreted as
    // a base-10000 number, where each component is an individual digit. this makes it simple to use integer
    // operations for comparing versions. for example (with componentCount = 4):
    //
    //   version "5.9.22.1" = 5*1000^3 + 9*1000^2 + 22*1000^1 + 1*1000^0 = 5000900220001
    //    and
    //   version "6.0.0.0" = 6*1000^3 + 0*1000^2 + 0*1000^1 + 0*1000^1 = 6000000000000
    //    and
    //   version "6" = 6*1000^3 + 0*1000^2 + 0*1000^1 + 0*1000^1 = 6000000000000
    //
    // then the integer comparisons hold true as you would expect:
    //
    //   "5.9.22.1" < "6.0.0.0" // true
    //   "6.0.0.0" == "6"       // true
    //

    static NSCharacterSet *nonDecimalDigitCharacter;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,
        ^{  // don't allocate this charset every time the function is called
            nonDecimalDigitCharacter = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
        });

    SInt64 base    = 10000; // each component in the version string must be less than base
    SInt64 result  =     0;
    SInt64 power   =     0;

    // construct the decimal value left-to-right from the version string
    for (NSString *component in [versionString componentsSeparatedByString:@"."])
    {
        if (NSNotFound != [component rangeOfCharacterFromSet:nonDecimalDigitCharacter].location)
        {
            // one of the version components is not an integer, so bail out
            result = -1;
            break;
        }
        result += [component longLongValue] * (long long)pow((double)base, (double)(componentCount - ++power));
    }

    return result;
}

+ (SInt64)integerFromVersionString:(NSString *)versionString
{
    return [[self class] integerFromVersionString:versionString
                               withComponentCount:[[versionString componentsSeparatedByString:@"."] count]];
}

+ (SInt64)integerFromiOSVersionString:(NSString *)versionString
{
    // iOS uses 3-component version string
    return [[self class] integerFromVersionString:versionString
                               withComponentCount:3];
}

It's somewhat future-proof in that it supports many revision identifiers (through 4 digits, 0-9999; change base to adjust this range) and can support an arbitrary number of components (Apple seems to use 3 components for now, e.g. major.minor.patch), but this can be specified explicitly using the componentCount argument. Be sure your componentCount and base do not cause overflow, i.e. ensure 2^63 >= base^componentCount!

Usage example:

NSString *currentVersion = [[UIDevice currentDevice] systemVersion];
if ([Util integerFromiOSVersionString:currentVersion] >= [Util integerFromiOSVersionString:@"42"])
{
    NSLog(@"we are in some horrible distant future where iOS still exists");
}
Arguelles answered 26/1, 2017 at 22:16 Comment(0)
G
-1

Add the Swift code below in your project and access information such as iOS version and device easily.

class DeviceInfo: NSObject {

    struct ScreenSize
    {
        static let SCREEN_WIDTH = UIScreen.main.bounds.size.width
        static let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
        static let SCREEN_MAX_LENGTH = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
        static let SCREEN_MIN_LENGTH = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
    }

    struct DeviceType
    {
        static let IS_IPHONE_4_OR_LESS =  UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
        static let IS_IPHONE_5 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
        static let IS_IPHONE_6 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH >= 667.0
        static let IS_IPHONE_6P = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
        static let IS_IPHONE_X = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 812.0
        static let IS_IPAD      = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0
        static let IS_IPAD_PRO  = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0
    }

    struct VersionType{
        static let SYS_VERSION_FLOAT = (UIDevice.current.systemVersion as NSString).floatValue
        static let iOS7 = (VersionType.SYS_VERSION_FLOAT < 8.0 && VersionType.SYS_VERSION_FLOAT >= 7.0)
        static let iOS8 = (VersionType.SYS_VERSION_FLOAT >= 8.0 && VersionType.SYS_VERSION_FLOAT < 9.0)
        static let iOS9 = (VersionType.SYS_VERSION_FLOAT >= 9.0 && VersionType.SYS_VERSION_FLOAT < 10.0)
        static let iOS10 = (VersionType.SYS_VERSION_FLOAT >= 9.0 && VersionType.SYS_VERSION_FLOAT < 11.0)
    }
}
Grivet answered 17/4, 2018 at 4:39 Comment(0)
D
-4

All answers look a bit to big. I just use:

if (SYSTEM_VERSION_GREATER_THAN(@"7.0")){(..CODE...)}
if (SYSTEM_VERSION_EQUAL_TO(@"7.0")){(..CODE...)}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")){(..CODE...)}
if (SYSTEM_VERSION_LESS_THAN(@"7.0")){(..CODE...)}
if (SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"7.0")){(..CODE...)}

Of course replace the @"7.0" with your required OS version.

Doloroso answered 17/10, 2013 at 8:41 Comment(3)
This does not answer anything.Rowlandson
@AndresCanella I guess the answer is if (SYSTEM_VERSION_GREATER_THAN(@"7.0")){(..CODE...)}, so it does answer the question, no?Templet
@Templet No, This does not answer the topic question. It suggests unclearly to use defines, but it does not include the actual code for the define. ex: #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) Answers should directly relate to the topic question and should be clear. That's why this got voted down.Rowlandson
T
-5
  1. From the the Home Screen, tap Settings > General > About.
  2. The software version of your device should appear on this screen.
  3. Check whether the version number is greater than 3.1.3.
Templet answered 9/1, 2015 at 9:9 Comment(3)
Can the people who down voted my answer please also provide a motivation to they did so? That would be helpful.Templet
You're being downvoted because your answer is not appropriate for this site. The question was how to check the device version using code, not a step-by-step on how to check the settings on the device. Your answer would be more suitable for Ask Different.Mme
@Mme But I mean, if this is a programming question, doesn't it make sense to at least say what programming language you are using?Templet

© 2022 - 2024 — McMap. All rights reserved.