How to check the OS version in Swift?


In this article, you will see how you can check the OS version to build the functionalities in the Swift language.

We will use the following methods to check the OS version in Swift −

  • Using the ProcessInfo class

  • Using the #available attribute

  • Using the UIDevice class

  • Using the @available attribute

Using the ProcessInfo class

The ProcessInfo class can be used to fetch the versions (Major, minor, and patch) in the Swift language. Below is an example to fetch the iOS version and print it to the console.

Example 

import Foundation
let osVersion = ProcessInfo.processInfo.operatingSystemVersion
print("OS version major: \(osVersion.majorVersion)")
print("OS version minor: \(osVersion.minorVersion)")
print("OS version patch: \(osVersion.patchVersion)")

Output

OS version major: 5
OS version minor: 15
OS version patch: 0

In the above code, ProcessInfo class gives access to the operatingSystemVersion property. The operatingSystemVersion property is a struct type that contains different properties to get versions. This property can be used to fetch the major, minor, and patch versions. This property gives all versions using different properties.

How to check the iOS version?

In Swift, you can use the UIDevice class to get the version of iOS. Here is an example of how to get the iOS version and print it to the console −

Example 

import UIKit
let device = UIDevice.current
let iosVersion = device.systemVersion
print("Current iOS version you are running: \(iosVersion)")

Output

Current iOS version you are running: 16.2

This code uses the systemVersion property of the UIDevice class to get a string containing the iOS version.

Check the iOS version Using the #available attribute

We can also use #available attribute to check the iOS version at runtime and execute different codes based on the version.

The code will only execute if the app is running on a version of iOS that is greater than or equal to the specified version.

Example

import Foundation
if #available(iOS 13, *) {
   print("Code to be executed on devices running iOS 13 or later")
} else {
   print("Code to be executed on devices running earlier versions of iOS")
}

Output

The above code will execute only in iOS 13 or higher, If the device runs an earlier version of iOS, the code inside the second block will be executed.

In our case, we are running the code on the iOS 16.2 version.

Code to be executed on devices running iOS 13 or later

You can also use the #available attribute for different platforms like watchOS, macOS, and tvOS. Here is an example of checking the watchOS version in the following −

import Foundation
if #available(watchOS 6, *) {
   // watchOS 6 specific code
} else {
   // watchOS 5 or earlier
}

This will check the current watchOS version and execute the code inside the if block if the current watchOS version is 6 or greater.

Check the iOS version Using the @available attribute

@available attribute is used to check the iOS version at runtime and execute different codes based on the version. It takes a version number and a message as an argument. in @available attribute, the code will be executed only if the app is running on a version of iOS that is greater than or equal to the specified version.

Example

@available(iOS 13, *)
func testFunction() {
   // Code to be executed on devices running iOS 13 or later
   self.imageView.image = UIImage(systemName: "play.fill")
}

In this example, the function testFunction will be executed only if the device is running iOS 13 or later.

It's also important to notice that if the function or class is not available for a lower version of the platform, the app will crash.

The @available and #available attributes in Swift are both used to check the version of the operating system at runtime and execute different code based on the version. However, they are used in different ways and have slightly different functionality.

@available is used to mark a function, class, or method as available only for a certain version or later. It takes two arguments: a version number and a message. If the app is running on a version of the operating system that is earlier than the specified version, the code inside the marked function, class, or method will not be executed and the app will crash.

When using #available, you can create conditional code that will only run if the app is running on an operating system version greater than or equal to the one you provide. The version number is the only parameter required. Only if the application is running on an operating system version greater than or equal to the given version will the code inside the #available block be put into action.

Conclusion

In this article, we learn how to check the iOS version. Based on that, how to run the code version specifics. There are different ways to check for different cases.

Updated on: 28-Feb-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements