How to Check iOS Version in Your Application

How to Check iOS Version in Your Application

22 May 2024 Stephan Petzl Leave a comment Tech-Help

As a developer, you might encounter situations where you need to check the iOS version of the device to ensure compatibility or to apply specific logic. This guide will walk you through various methods to check the iOS version, helping you choose the best approach for your project.

Using Preprocessor Macros

One of the most reliable ways to check the iOS version is by using preprocessor macros. This method is highly recommended for its simplicity and effectiveness.


    /* System Versioning Preprocessor Macros */
    
    #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
    #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
    
    /* Usage */
    
    if (SYSTEM_VERSION_LESS_THAN(@"4.0")) {
        // Code for iOS versions less than 4.0
    }
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"3.1.1")) {
        // Code for iOS versions 3.1.1 and above
    }
    

Using Availability Checking in Swift and Objective-C

With the introduction of Swift 2.0 and later versions of Objective-C, Apple provided a more streamlined way to check for iOS versions using the #available keyword.

In Swift


    if #available(iOS 9, *) {
        // Code for iOS 9 and above
    } else {
        // Code for iOS 8 and below
    }
    

In Objective-C


    if (@available(iOS 9, *)) {
        // Code for iOS 9 and above
    } else {
        // Code for iOS 8 and below
    }
    

Comparing System Versions Directly

For projects targeting iOS 8 and above, you can use the NSProcessInfo class to perform version comparisons.


    NSOperatingSystemVersion version = {8, 0, 0};
    if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:version]) {
        // Code for iOS 8 and above
    } else {
        // Code for versions below iOS 8
    }
    

For older versions, you can use the systemVersion property of UIDevice to compare versions as strings:


    NSString *reqSysVer = @"3.1";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
        // Code for iOS 3.1 and above
    }
    

Using NSFoundationVersionNumber

Another approach is to use the NSFoundationVersionNumber constant. This method is straightforward and recommended by Apple’s official documentation.


    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        // Code for iOS 7 and above
    }
    

Why Choose the Right Method?

Choosing the right method to check the iOS version is crucial for maintaining compatibility and ensuring that your app runs smoothly across different iOS versions. It’s also essential for leveraging new features and APIs introduced in newer versions without breaking functionality on older devices.

Enhance Your Testing with Repeato

As you’re developing and testing your iOS applications, it’s essential to ensure that your tests are comprehensive and efficient. This is where Repeato can make a significant difference. Repeato is a No-code test automation tool for iOS and Android that helps you create, run, and maintain automated tests for your apps.

With Repeato, you can quickly edit and run tests, thanks to its use of computer vision and AI. This allows developers to focus on creating a great product instead of spending time on creating and maintaining tests. Additionally, Repeato enables non-technical colleagues or QAs to handle test automation tasks, freeing up valuable developer resources.

For more information on how Repeato can enhance your development and testing workflow, visit our documentation or contact us.

Like this article? there’s more where that came from!