How to check iOS version?

后端 未结 30 2427
一生所求
一生所求 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:12

    Solution for checking iOS version in Swift

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

    Con of this solution: it is simply bad practice to check against OS version numbers, whichever way you do it. One should never hard code dependencies in this way, always check for features, capabilities or the existence of a class. Consider this; Apple may release a backwards compatible version of a class, if they did then the code you suggest would never use it as your logic looks for an OS version number and NOT the existence of the class.

    (Source of this information)

    Solution for checking the class existence in Swift

    if (objc_getClass("UIAlertController") == nil) {
       // iOS 7
    } else {
       // iOS 8+
    }
    

    Do not use if (NSClassFromString("UIAlertController") == nil) because it works correctly on the iOS simulator using iOS 7.1 and 8.2, but if you test on a real device using iOS 7.1, you will unfortunately notice that you will never pass through the else part of the code snippet.

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

    There are version like 7.0 or 6.0.3, so we can simply convert version into numerics to compare. if version is like 7.0, simply append another ".0" to it and then take its numeric value.

     int version;
     NSString* iosVersion=[[UIDevice currentDevice] systemVersion];
     NSArray* components=[iosVersion componentsSeparatedByString:@"."];
     if ([components count]==2) {
        iosVersion=[NSString stringWithFormat:@"%@.0",iosVersion];
    
     }
     iosVersion=[iosVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
     version=[iosVersion integerValue];
    

    For 6.0.0

      if (version==600) {
        // Do something
      }
    

    for 7.0

     if (version==700) {
       // Do something
     }
    
    0 讨论(0)
  • 2020-11-21 07:15
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
            // Your code here
    }
    

    Where of course, NSFoundationVersionNumber_iOS_6_1 must be changed to by applicable for the iOS version you want to check. What I have now written will probably be used a lot when testing if a device is running iOS7 or a previous version.

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

    I know this is an old question, but someone should have mentioned the compile-time macros in Availability.h. All of the other methods here are runtime solutions, and will not work in a header file, class category, or ivar definition.

    For these situations, use

    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_6_0
      // iOS 6+ code here
    #else
      // Pre iOS 6 code here
    #endif
    

    h/t this answer

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

    Using the refered recommended way... if there is no definition in the header files, you can always get the versión printing it on console with a device of the desired IOS versión.

    - (BOOL) isIOS8OrAbove{
        float version802 = 1140.109985;
        float version8= 1139.100000; // there is no def like NSFoundationVersionNumber_iOS_7_1 for ios 8 yet?
        NSLog(@"la version actual es [%f]", NSFoundationVersionNumber);
        if (NSFoundationVersionNumber >= version8){
            return true;
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-11-21 07:17
    float deviceOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
    float versionToBeCompared = 3.1.3; //(For Example in your case)
    
    if(deviceOSVersion < versionToBeCompared)
       //Do whatever you need to do. Device version is lesser than 3.1.3(in your case)
    else 
       //Device version should be either equal to the version you specified or above
    
    0 讨论(0)
提交回复
热议问题