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?
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?
Then add a if condition as follows:-
Try:
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]
returnsNSOrderedDescending
which might well not be intended at all; I might expectNSOrderedSame
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
andNSOrderedDescending
but I can think of a use for some thing calledNSOrderedUnordered
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.
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:
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
This would yield the following:
Deploying on iOS 7
This would yield:
More reading at NSHipster.
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; }
).Swift example that actually works:
Don't use NSProcessInfo cause it doesn't work under 8.0, so its pretty much useless until 2016