How to check iOS version?

后端 未结 30 2397
一生所求
一生所求 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:01
    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
    

    Then add a if condition as follows:-

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
       //Your code
    }       
    
    0 讨论(0)
  • 2020-11-21 07:03

    New way to check the system version using the swift Forget [[UIDevice currentDevice] systemVersion] and NSFoundationVersionNumber.

    We can use NSProcessInfo -isOperatingSystemAtLeastVersion

         import Foundation
    
         let yosemite = NSOperatingSystemVersion(majorVersion: 10, minorVersion: 10, patchVersion: 0)
         NSProcessInfo().isOperatingSystemAtLeastVersion(yosemite) // false
    
    0 讨论(0)
  • 2020-11-21 07:04

    In general it's better to ask if an object can perform a given selector, rather than checking a version number to decide if it must be present.

    When this is not an option, you do need to be a bit careful here because [@"5.0" compare:@"5" options:NSNumericSearch] returns NSOrderedDescending which might well not be intended at all; I might expect NSOrderedSame here. This is at least a theoretical concern, one that is worth defending against in my opinion.

    Also worth considering is the possibility of a bad version input which can not reasonably be compared to. Apple supplies the three predefined constants NSOrderedAscending, NSOrderedSame and NSOrderedDescending but I can think of a use for some thing called NSOrderedUnordered in the event I can't compare two things and I want to return a value indicating this.

    What's more, it's not impossible that Apple will some day expand their three predefined constants to allow a variety of return values, making a comparison != NSOrderedAscending unwise.

    With this said, consider the following code.

    typedef enum {kSKOrderedNotOrdered = -2, kSKOrderedAscending = -1, kSKOrderedSame = 0, kSKOrderedDescending = 1} SKComparisonResult;
    
    @interface SKComparator : NSObject
    + (SKComparisonResult)comparePointSeparatedVersionNumber:(NSString *)vOne withPointSeparatedVersionNumber:(NSString *)vTwo;
    @end
    
    @implementation SKComparator
    + (SKComparisonResult)comparePointSeparatedVersionNumber:(NSString *)vOne withPointSeparatedVersionNumber:(NSString *)vTwo {
      if (!vOne || !vTwo || [vOne length] < 1 || [vTwo length] < 1 || [vOne rangeOfString:@".."].location != NSNotFound ||
        [vTwo rangeOfString:@".."].location != NSNotFound) {
        return SKOrderedNotOrdered;
      }
      NSCharacterSet *numericalCharSet = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];
      NSString *vOneTrimmed = [vOne stringByTrimmingCharactersInSet:numericalCharSet];
      NSString *vTwoTrimmed = [vTwo stringByTrimmingCharactersInSet:numericalCharSet];
      if ([vOneTrimmed length] > 0 || [vTwoTrimmed length] > 0) {
        return SKOrderedNotOrdered;
      }
      NSArray *vOneArray = [vOne componentsSeparatedByString:@"."];
      NSArray *vTwoArray = [vTwo componentsSeparatedByString:@"."];
      for (NSUInteger i = 0; i < MIN([vOneArray count], [vTwoArray count]); i++) {
        NSInteger vOneInt = [[vOneArray objectAtIndex:i] intValue];
        NSInteger vTwoInt = [[vTwoArray objectAtIndex:i] intValue];
        if (vOneInt > vTwoInt) {
          return kSKOrderedDescending;
        } else if (vOneInt < vTwoInt) {
          return kSKOrderedAscending;
        }
      }
      if ([vOneArray count] > [vTwoArray count]) {
        for (NSUInteger i = [vTwoArray count]; i < [vOneArray count]; i++) {
          if ([[vOneArray objectAtIndex:i] intValue] > 0) {
            return kSKOrderedDescending;
          }
        }
      } else if ([vOneArray count] < [vTwoArray count]) {
        for (NSUInteger i = [vOneArray count]; i < [vTwoArray count]; i++) {
          if ([[vTwoArray objectAtIndex:i] intValue] > 0) {
            return kSKOrderedAscending;
          }
        }
      }
      return kSKOrderedSame;
    }
    @end
    
    0 讨论(0)
  • 2020-11-21 07:04
    #define IsIOS8 (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
    
    0 讨论(0)
  • 2020-11-21 07:04

    Try this

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { 
    // do some work
    }
    
    0 讨论(0)
  • 2020-11-21 07:07

    This is used to check for compatible SDK version in Xcode, this is if you have a large team with different versions of Xcode or multiple projects supporting different SDKs that share the same code:

    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
      //programming in iOS 8+ SDK here
    #else
      //programming in lower than iOS 8 here   
    #endif
    

    What you really want is to check the iOS version on the device. You can do that with this:

    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
      //older than iOS 8 code here
    } else {
      //iOS 8 specific code here
    }
    

    Swift version:

    if let version = Float(UIDevice.current.systemVersion), version < 9.3 {
        //add lower than 9.3 code here
    } else {
        //add 9.3 and above code here
    }
    

    Current versions of swift should be using this:

    if #available(iOS 12, *) {
        //iOS 12 specific code here
    } else {
        //older than iOS 12 code here
    }
    
    0 讨论(0)
提交回复
热议问题