How to check iOS version?

后端 未结 30 2430
一生所求
一生所求 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: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 
    
    - (std::vector) splitSystemVersion {
        std::string version = [[[UIDevice currentDevice] systemVersion] UTF8String];
        std::vector 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(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) 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(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(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;
    }
    

提交回复
热议问题