Check OS version in Swift?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In this article, we'll delve into checking the operating system (OS) version in Swift, a critical aspect when developing iOS apps that need compatibility across different OS versions. By identifying the OS version, developers can conditionally execute code, ensuring the app behaves optimally on both the latest and older versions of iOS. We will cover the ways to check OS versions, including usage examples and some best practices.
Checking OS Versions in Swift
Using ProcessInfo
One of the most common methods for checking the system version in Swift is by using the ProcessInfo class. ProcessInfo provides information about the current environment, including the operating system.
Here's how you can use it to check the OS version:
The operatingSystemVersion property of ProcessInfo returns an OperatingSystemVersion struct, which has majorVersion, minorVersion, and patchVersion properties.
Using Availability Conditions
Swift provides a powerful feature called availability conditions, allowing you to write code that is only executed if certain OS versions are met. This is done using the #available keyword.
Example of using availability conditions:
The #available directive is simple and effective for checking and executing code based on OS version. The asterisk (*) serves as a wildcard for other platforms.
@available Attribute
In addition to using #available in conditions, Swift also offers an @available attribute that can be used with classes, methods, and variables to specify availability constraints.
Example:
This method makes functions or properties available only when running on the specified versions.
Comparing Versions with OperatingSystemVersion
When you need to compare specific versions not covered by the high-level #available directive, you can use OperatingSystemVersion.
Example:
Comparing two OperatingSystemVersion instances is straightforward as the method supports comparison operators like >=, > , <=, and <.
Summary
The following table summarizes the key points of checking OS versions in Swift:
| Method/Class | Description | Usage |
ProcessInfo | Provides the operatingSystemVersion for detailed version checks. | ProcessInfo.processInfo.operatingSystemVersion |
#available | High-level syntax for checking availability of features across iOS versions. | if #available(iOS 15, *) { ... } |
@available | Declares the availability of classes/methods for specific OS versions using the attribute syntax. | @available(iOS 15, *) |
OperatingSystemVersion | Struct available via ProcessInfo, useful for custom version handling and comparison. | OperatingSystemVersion(major: minor: patch:) |
Best Practices
- Use
#availablefor Feature Checks: When you only need to check for API availability rather than specific version numbers, prefer using#available. - Backward Compatibility: Always ensure that your application handles older versions gracefully, potentially disabling certain features or providing alternative solutions.
- Code Organization: Group code by version and use clear documentation/comments indicating which parts are applicable to specific OS versions.
- Testing: Regularly test your application on simulators and devices running different iOS versions to avoid unexpected behavior.
- Performance Consideration: Checking for OS versions might have a negligible performance impact, but structuring your conditions cleanly will maintain code performance and readability.
By understanding and using these methods of checking the OS version in Swift, developers can create more robust, flexible, and compatible iOS applications. Make sure to stay updated on changes in the Swift language and iOS SDK that may introduce new ways of handling OS versions.

