How to check iOS version?

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

    Try:

    NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"3.1.3" options: NSNumericSearch];
    if (order == NSOrderedSame || order == NSOrderedDescending) {
        // OS version >= 3.1.3
    } else {
        // OS version < 3.1.3
    }
    
    0 讨论(0)
  • 2020-11-21 07:21

    UIDevice+IOSVersion.h

    @interface UIDevice (IOSVersion)
    
    + (BOOL)isCurrentIOSVersionEqualToVersion:(NSString *)iOSVersion;
    + (BOOL)isCurrentIOSVersionGreaterThanVersion:(NSString *)iOSVersion;
    + (BOOL)isCurrentIOSVersionGreaterThanOrEqualToVersion:(NSString *)iOSVersion;
    + (BOOL)isCurrentIOSVersionLessThanVersion:(NSString *)iOSVersion;
    + (BOOL)isCurrentIOSVersionLessThanOrEqualToVersion:(NSString *)iOSVersion
    
    @end
    

    UIDevice+IOSVersion.m

    #import "UIDevice+IOSVersion.h"
    
    @implementation UIDevice (IOSVersion)
    
    + (BOOL)isCurrentIOSVersionEqualToVersion:(NSString *)iOSVersion
    {
        return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] == NSOrderedSame;
    }
    
    + (BOOL)isCurrentIOSVersionGreaterThanVersion:(NSString *)iOSVersion
    {
        return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] == NSOrderedDescending;
    }
    
    + (BOOL)isCurrentIOSVersionGreaterThanOrEqualToVersion:(NSString *)iOSVersion
    {
        return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] != NSOrderedAscending;
    }
    
    + (BOOL)isCurrentIOSVersionLessThanVersion:(NSString *)iOSVersion
    {
        return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] == NSOrderedAscending;
    }
    
    + (BOOL)isCurrentIOSVersionLessThanOrEqualToVersion:(NSString *)iOSVersion
    {
        return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] != NSOrderedDescending;
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-21 07:21

    As a variation of yasimturks solution, I defined one function and a few enum values instead of five macros. I find it more elegant, but that's a matter of taste.

    Usage:

    if (systemVersion(LessThan, @"5.0")) ...
    

    .h file:

    typedef enum {
      LessThan,
      LessOrEqual,
      Equal,
      GreaterOrEqual,
      GreaterThan,
      NotEqual
    } Comparison;
    
    BOOL systemVersion(Comparison test, NSString* version);
    

    .m file:

    BOOL systemVersion(Comparison test, NSString* version) {
      NSComparisonResult result = [[[UIDevice currentDevice] systemVersion] compare: version options: NSNumericSearch];
      switch (test) {
        case LessThan:       return result == NSOrderedAscending;
        case LessOrEqual:    return result != NSOrderedDescending;
        case Equal:          return result == NSOrderedSame;
        case GreaterOrEqual: return result != NSOrderedAscending;
        case GreaterThan:    return result == NSOrderedDescending;
        case NotEqual:       return result != NSOrderedSame;
      }
    }
    

    You should add your app's prefix to the names, especially to the Comparison type.

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

    Preferred Approach

    In Swift 2.0 Apple added availability checking using a far more convenient syntax (Read more here). Now you can check the OS version with a cleaner syntax:

    if #available(iOS 9, *) {
        // Then we are on iOS 9
    } else {
        // iOS 8 or earlier
    }
    

    This is the preferred over checking respondsToSelector etc (What's New In Swift). Now the compiler will always warn you if you aren't guarding your code properly.


    Pre Swift 2.0

    New in iOS 8 is NSProcessInfo allowing for better semantic versioning checks.

    Deploying on iOS 8 and greater

    For minimum deployment targets of iOS 8.0 or above, use NSProcessInfo operatingSystemVersion or isOperatingSystemAtLeastVersion.

    This would yield the following:

    let minimumVersion = NSOperatingSystemVersion(majorVersion: 8, minorVersion: 1, patchVersion: 2)
    if NSProcessInfo().isOperatingSystemAtLeastVersion(minimumVersion) {
        //current version is >= (8.1.2)
    } else {
        //current version is < (8.1.2)
    }
    

    Deploying on iOS 7

    For minimum deployment targets of iOS 7.1 or below, use compare with NSStringCompareOptions.NumericSearch on UIDevice systemVersion.

    This would yield:

    let minimumVersionString = "3.1.3"
    let versionComparison = UIDevice.currentDevice().systemVersion.compare(minimumVersionString, options: .NumericSearch)
    switch versionComparison {
        case .OrderedSame, .OrderedDescending:
            //current version is >= (3.1.3)
            break
        case .OrderedAscending:
            //current version is < (3.1.3)
            fallthrough
        default:
            break;
    }
    

    More reading at NSHipster.

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

    I always keep those in my Constants.h file:

    #define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES) 
    #define IS_OS_5_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
    #define IS_OS_6_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
    #define IS_OS_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    #define IS_OS_8_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    
    0 讨论(0)
  • 2020-11-21 07:24

    A more generic version in Obj-C++ 11 (you could probably replace some of this stuff with the NSString/C functions, but this is less verbose. This gives you two mechanisms. splitSystemVersion gives you an array of all the parts which is useful if you just want to switch on the major version (e.g. switch([self splitSystemVersion][0]) {case 4: break; case 5: break; }).

    #include <boost/lexical_cast.hpp>
    
    - (std::vector<int>) splitSystemVersion {
        std::string version = [[[UIDevice currentDevice] systemVersion] UTF8String];
        std::vector<int> versions;
        auto i = version.begin();
    
        while (i != version.end()) {
            auto nextIllegalChar = std::find_if(i, version.end(), [] (char c) -> bool { return !isdigit(c); } );
            std::string versionPart(i, nextIllegalChar);
            i = std::find_if(nextIllegalChar, version.end(), isdigit);
    
            versions.push_back(boost::lexical_cast<int>(versionPart));
        }
    
        return versions;
    }
    
    /** Losslessly parse system version into a number
     * @return <0>: the version as a number,
     * @return <1>: how many numeric parts went into the composed number. e.g.
     * X.Y.Z = 3.  You need this to know how to compare again <0>
     */
    - (std::tuple<int, int>) parseSystemVersion {
        std::string version = [[[UIDevice currentDevice] systemVersion] UTF8String];
        int versionAsNumber = 0;
        int nParts = 0;
    
        auto i = version.begin();
        while (i != version.end()) {
            auto nextIllegalChar = std::find_if(i, version.end(), [] (char c) -> bool { return !isdigit(c); } );
            std::string versionPart(i, nextIllegalChar);
            i = std::find_if(nextIllegalChar, version.end(), isdigit);
    
            int part = (boost::lexical_cast<int>(versionPart));
            versionAsNumber = versionAsNumber * 100 + part;
            nParts ++;
        }
    
        return {versionAsNumber, nParts};
    }
    
    
    /** Assume that the system version will not go beyond X.Y.Z.W format.
     * @return The version string.
     */
    - (int) parseSystemVersionAlt {
        std::string version = [[[UIDevice currentDevice] systemVersion] UTF8String];
        int versionAsNumber = 0;
        int nParts = 0;
    
        auto i = version.begin();
        while (i != version.end() && nParts < 4) {
            auto nextIllegalChar = std::find_if(i, version.end(), [] (char c) -> bool { return !isdigit(c); } );
            std::string versionPart(i, nextIllegalChar);
            i = std::find_if(nextIllegalChar, version.end(), isdigit);
    
            int part = (boost::lexical_cast<int>(versionPart));
            versionAsNumber = versionAsNumber * 100 + part;
            nParts ++;
        }
    
        // don't forget to pad as systemVersion may have less parts (i.e. X.Y).
        for (; nParts < 4; nParts++) {
            versionAsNumber *= 100;
        }
    
        return versionAsNumber;
    }
    
    0 讨论(0)
提交回复
热议问题