Check OS version in Swift? Check OS version in Swift? swift swift

Check OS version in Swift?


For iOS, try:

var systemVersion = UIDevice.current.systemVersion

For OS X, try:

var systemVersion = NSProcessInfo.processInfo().operatingSystemVersion

If you just want to check if the users is running at least a specific version, you can also use the following Swift 2 feature which works on iOS and OS X:

if #available(iOS 9.0, *) {    // use the feature only available in iOS 9    // for ex. UIStackView} else {    // or use some work around}

BUT it is not recommended to check the OS version. It is better to check if the feature you want to use is available on the device than comparing version numbers.For iOS, as mentioned above, you should check if it responds to a selector; eg.:

if (self.respondsToSelector(Selector("showViewController"))) {    self.showViewController(vc, sender: self)} else {    // some work around}


Update:
Now you should use new availability checking introduced with Swift 2:
e.g. To check for iOS 9.0 or later at compile time use this:

if #available(iOS 9.0, *) {    // use UIStackView} else {    // show sad face emoji}

or can be used with whole method or class

@available(iOS 9.0, *)func useStackView() {    // use UIStackView}

For more info see this.

Run time checks:

if you don't want exact version but want to check iOS 9,10 or 11 using if:

let floatVersion = (UIDevice.current.systemVersion as NSString).floatValue

EDIT:Just found another way to achieve this:

let iOS8 = floor(NSFoundationVersionNumber) > floor(NSFoundationVersionNumber_iOS_7_1)let iOS7 = floor(NSFoundationVersionNumber) <= floor(NSFoundationVersionNumber_iOS_7_1)


I made helper functions that were transferred from the below link into swift:

How can we programmatically detect which iOS version is device running on?

func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool {    return UIDevice.currentDevice().systemVersion.compare(version,        options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedSame}func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool {    return UIDevice.currentDevice().systemVersion.compare(version,        options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedDescending}func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {    return UIDevice.currentDevice().systemVersion.compare(version,        options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedAscending}func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool {    return UIDevice.currentDevice().systemVersion.compare(version,        options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedAscending}func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {    return UIDevice.currentDevice().systemVersion.compare(version,        options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedDescending}

It can be used like so:

SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO("7.0")

Swift 4.2

func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool {    return UIDevice.current.systemVersion.compare(version, options: .numeric) == .orderedSame}func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool {    return UIDevice.current.systemVersion.compare(version, options: .numeric) == .orderedDescending}func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {    return UIDevice.current.systemVersion.compare(version, options: .numeric) != .orderedAscending}func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool {    return UIDevice.current.systemVersion.compare(version, options: .numeric) == .orderedAscending}func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {    return UIDevice.current.systemVersion.compare(version, options: .numeric) != .orderedDescending}