How to check iOS version?

后端 未结 30 2420
一生所求
一生所求 2020-11-21 06:37

I want to check if the iOS version of the device is greater than 3.1.3 I tried things like:

[[UIDevice currentDevice].systemVersion         


        
相关标签:
30条回答
  • 2020-11-21 07:07

    With Version class that is contained in nv-ios-version project (Apache License, Version 2.0), it is easy to get and compare iOS version. An example code below dumps the iOS version and checks whether the version is greater than or equal to 6.0.

    // Get the system version of iOS at runtime.
    NSString *versionString = [[UIDevice currentDevice] systemVersion];
    
    // Convert the version string to a Version instance.
    Version *version = [Version versionWithString:versionString];
    
    // Dump the major, minor and micro version numbers.
    NSLog(@"version = [%d, %d, %d]",
        version.major, version.minor, version.micro);
    
    // Check whether the version is greater than or equal to 6.0.
    if ([version isGreaterThanOrEqualToMajor:6 minor:0])
    {
        // The iOS version is greater than or equal to 6.0.
    }
    
    // Another way to check whether iOS version is
    // greater than or equal to 6.0.
    if (6 <= version.major)
    {
        // The iOS version is greater than or equal to 6.0.
    }
    

    Project Page: nv-ios-version
    TakahikoKawasaki/nv-ios-version

    Blog: Get and compare iOS version at runtime with Version class
    Get and compare iOS version at runtime with Version class

    0 讨论(0)
  • 2020-11-21 07:08

    The quick answer …


    As of Swift 2.0, you can use #available in an if or guard to protect code that should only be run on certain systems.

    if #available(iOS 9, *) {}


    In Objective-C, you need to check the system version and perform a comparison.

    [[NSProcessInfo processInfo] operatingSystemVersion] in iOS 8 and above.

    As of Xcode 9:

    if (@available(iOS 9, *)) {}


    The full answer …

    In Objective-C, and Swift in rare cases, it's better to avoid relying on the operating system version as an indication of device or OS capabilities. There is usually a more reliable method of checking whether a particular feature or class is available.

    Checking for the presence of APIs:

    For example, you can check if UIPopoverController is available on the current device using NSClassFromString:

    if (NSClassFromString(@"UIPopoverController")) {
        // Do something
    }
    

    For weakly linked classes, it is safe to message the class, directly. Notably, this works for frameworks that aren't explicitly linked as "Required". For missing classes, the expression evaluates to nil, failing the condition:

    if ([LAContext class]) {
        // Do something
    }
    

    Some classes, like CLLocationManager and UIDevice, provide methods to check device capabilities:

    if ([CLLocationManager headingAvailable]) {
        // Do something
    }
    

    Checking for the presence of symbols:

    Very occasionally, you must check for the presence of a constant. This came up in iOS 8 with the introduction of UIApplicationOpenSettingsURLString, used to load Settings app via -openURL:. The value didn't exist prior to iOS 8. Passing nil to this API will crash, so you must take care to verify the existence of the constant first:

    if (&UIApplicationOpenSettingsURLString != NULL) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }
    

    Comparing against the operating system version:

    Let's assume you're faced with the relatively rare need to check the operating system version. For projects targeting iOS 8 and above, NSProcessInfo includes a method for performing version comparisons with less chance of error:

    - (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version
    

    Projects targeting older systems can use systemVersion on UIDevice. Apple uses it in their GLSprite sample code.

    // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
    // class is used as fallback when it isn't available.
    NSString *reqSysVer = @"3.1";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
        displayLinkSupported = TRUE;
    }
    

    If for whatever reason you decide that systemVersion is what you want, make sure to treat it as an string or you risk truncating the patch revision number (eg. 3.1.2 -> 3.1).

    0 讨论(0)
  • 2020-11-21 07:09

    Try the below code:

    NSString *versionString = [[UIDevice currentDevice] systemVersion];
    
    0 讨论(0)
  • 2020-11-21 07:11
    #define _kisiOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    
    if (_kisiOS7) {
                NSLog(@"iOS7 or greater")
    } 
    else {
               NSLog(@"Less than iOS7");
    }
    
    0 讨论(0)
  • 2020-11-21 07:11

    Just for retrieving the OS version string value:

    [[UIDevice currentDevice] systemVersion]
    
    0 讨论(0)
  • 2020-11-21 07:11

    Swift example that actually works:

    switch UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch) {
    case .OrderedSame, .OrderedDescending:
        println("iOS >= 8.0")
    case .OrderedAscending:
        println("iOS < 8.0")
    }
    

    Don't use NSProcessInfo cause it doesn't work under 8.0, so its pretty much useless until 2016

    0 讨论(0)
提交回复
热议问题