I want to check if the iOS
version of the device is greater than 3.1.3
I tried things like:
[[UIDevice currentDevice].systemVersion
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.
New in iOS 8 is NSProcessInfo
allowing for better semantic versioning checks.
For minimum deployment targets of iOS 8.0 or above, use
NSProcessInfo
operatingSystemVersion
orisOperatingSystemAtLeastVersion
.
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)
}
For minimum deployment targets of iOS 7.1 or below, use compare with
NSStringCompareOptions.NumericSearch
onUIDevice 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.