How to check iOS version?

后端 未结 30 2418
一生所求
一生所求 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 06:57

    As suggested by the official Apple docs: you can use the NSFoundationVersionNumber, from the NSObjCRuntime.h header file.

    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        // here you go with iOS 7
    }
    
    0 讨论(0)
  • a bit late to the party but in light of iOS 8.0 out there this might be relevant:

    if you can avoid using

    [[UIDevice currentDevice] systemVersion]

    Instead check for existence of of a method/class/whatever else.

    if ([self.yourClassInstance respondsToSelector:@selector(<yourMethod>)]) 
    { 
        //do stuff 
    }
    

    I found it to be useful for location manager where I have to call requestWhenInUseAuthorization for iOS 8.0 but the method is not available for iOS < 8

    0 讨论(0)
  • 2020-11-21 06:58

    Starting Xcode 9, in Objective-C:

    if (@available(iOS 11, *)) {
        // iOS 11 (or newer) ObjC code
    } else {
        // iOS 10 or older code
    }
    

    Starting Xcode 7, in Swift:

    if #available(iOS 11, *) {
        // iOS 11 (or newer) Swift code
    } else {
        // iOS 10 or older code
    }
    

    For the version, you can specify the MAJOR, the MINOR or the PATCH (see http://semver.org/ for definitions). Examples:

    • iOS 11 and iOS 11.0 are the same minimal version
    • iOS 10, iOS 10.3, iOS 10.3.1 are different minimal versions

    You can input values for any of those systems:

    • iOS, macOS, watchOS, tvOS

    Real case example taken from one of my pods:

    if #available(iOS 10.0, tvOS 10.0, *) {
        // iOS 10+ and tvOS 10+ Swift code
    } else {
        // iOS 9 and tvOS 9 older code
    }
    

    documentation

    0 讨论(0)
  • 2020-11-21 06:58
    +(BOOL)doesSystemVersionMeetRequirement:(NSString *)minRequirement{
    
    // eg  NSString *reqSysVer = @"4.0";
    
    
      NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    
      if ([currSysVer compare:minRequirement options:NSNumericSearch] != NSOrderedAscending)
      {
        return YES;
      }else{
        return NO;
      }
    
    
    }
    
    0 讨论(0)
  • 2020-11-21 06:59
    /*
     *  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")) {
        ...
    }
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"3.1.1")) {
        ...
    }
    
    0 讨论(0)
  • 2020-11-21 06:59

    Here is a swift version:

    struct iOSVersion {
        static let SYS_VERSION_FLOAT = (UIDevice.currentDevice().systemVersion as NSString).floatValue
        static let iOS7 = (Version.SYS_VERSION_FLOAT < 8.0 && Version.SYS_VERSION_FLOAT >= 7.0)
        static let iOS8 = (Version.SYS_VERSION_FLOAT >= 8.0 && Version.SYS_VERSION_FLOAT < 9.0)
        static let iOS9 = (Version.SYS_VERSION_FLOAT >= 9.0 && Version.SYS_VERSION_FLOAT < 10.0)
    }
    

    Usage:

    if iOSVersion.iOS8 {
        //Do iOS8 code here
    }
    
    0 讨论(0)
提交回复
热议问题