As an iOS developer, it is crucial to know the version of your device’s operating system (OS) before you start developing a new app. The OS can greatly affect how your code will run and which features are available on the device.
1. Using the “version” Information Property in Xcode
The most straightforward way to determine the iOS version is by using the “version” information property in Xcode. This property returns a string that contains the major and minor versions of the OS installed on your device, along with any build numbers or additional information.
For example, if you are running iOS 13.4.1 on an iPhone XS Max, the “version” property would return the following string:
go
13.4.1
This method is straightforward and easy to use, especially for developers who are already working in Xcode. However, it has some limitations.
For instance, this method only works on devices that have Xcode installed. Additionally, it may not be reliable if the device’s OS version is not correctly reported by Xcode or if there are any custom modifications made to the device.
2. Using the “systemVersion” Property in Swift and Objective-C
Another way to determine the iOS version is by using the “systemVersion” property in Swift or Objective-C. This property returns a string that contains the major and minor versions of the OS installed on your device, along with any build numbers or additional information.
For example, if you are running iOS 13.4.1 on an iPhone XS Max, the “systemVersion” property would return the following string:
go
13.4.1
This method is more robust than using the “version” information property in Xcode as it works on any device that has a Swift or Objective-C compiler installed. However, like Xcode’s “version” property, this method may not be reliable if there are any custom modifications made to the device or if the device’s OS version is not correctly reported by the compiler.
3. Using the “userInfo” Dictionary in Swift
The “userInfo” dictionary is a built-in dictionary in Swift that contains information about the user, their device, and the iOS version. This information is stored in key-value pairs and can be accessed using the “UserDefaults” class in Swift.
To access the iOS version using the "userInfo" dictionary, you can use the following code:
swift
let osVersion UserDefaults.standard.string(forKey: “CFBundleShortVersionString”)
print(“iOS version: (osVersion ?? “Unknown”)”)
This method is reliable and does not require any additional libraries or dependencies to be installed.
However, it may not provide as much information as the “systemVersion” property in Swift or Objective-C. Additionally, this method requires accessing user preferences, which may not be suitable for all use cases.
4. Using the “UIDevice” Class in Objective-C
The “UIDevice” class is a built-in class in Objective-C that provides information about the user’s device, including its iOS version. To access the iOS version using the “UIDevice” class, you can use the following code:
objectivec
NSString *osVersion [[UIDevice currentDevice] systemVersion];
NSLog(@”iOS version: %@”, osVersion);
This method is reliable and does not require any additional libraries or dependencies to be installed.
However, it may not provide as much information as the “systemVersion” property in Swift or Objective-C. Additionally, this method requires accessing user preferences, which may not be suitable for all use cases.
5. Using the “System Profiler” App on iOS
The “System Profiler” app is a built-in app that comes with every iOS device. This app allows you to view detailed information about your device, including its iOS version and other specifications. To use this app, open it from the home screen and select “About This iPhone” or “About iPad/iPod touch”.
This method is straightforward and does not require any additional software or libraries to be installed.
However, it may not provide real-time information as the device’s OS version may have changed since the app was last updated. Additionally, this method requires physical access to the device.
6. Using the “DetectOS” Library in Swift
The “DetectOS” library is a third-party library in Swift that provides an easy way to determine the iOS version on your device. This library uses various techniques, such as checking the “systemVersion” property and the “version” information property in Xcode, to determine the OS version accurately.
To use this library, you can install it using CocoaPods or Carthage, and then import it into your Swift project:
swift
import DetectOS
After importing the library, you can use the following code to determine the iOS version:
swift
if let osVersion try? DetectOS.current()?.osVersion {
print(“iOS version: (osVersion)”)
} else {