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}
// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer// class is used as fallback when it isn't available.NSString *reqSysVer = @"3.1";NSString *currSysVer = [[UIDevice currentDevice] systemVersion];if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {displayLinkSupported = TRUE;}
Obj-C++11中的一个更通用的版本(您可能可以用NSString/C函数替换其中的一些东西,但这不太冗长。这给了您两种机制。分裂系统版本为您提供了一个所有部分的数组,如果您只想打开主要版本(例如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;}
BOOL shouldBranch = [SharedClass iOSMeetsOrExceedsVersion:5.0101];
(+) (BOOL) iOSMeetsOrExceedsVersion:(float)targetVersion {
/*Note: the incoming targetVersion should use 2 digits for each subVersion --
example 5.01 for v5.1, 5.11 for v5.11 (aka subversions above 9), 5.0101 for v5.1.1, etc.*/
// Logic: as a string, system version may have more than 2 segments (example: 5.1.1)// so, a direct conversion to a float may return an invalid number// instead, parse each part directly
NSArray *sysVersion = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];float floatVersion = [[sysVersion objectAtIndex:0] floatValue];if (sysVersion.count > 1) {NSString* subVersion = [sysVersion objectAtIndex:1];if (subVersion.length == 1)floatVersion += ([[sysVersion objectAtIndex:1] floatValue] *0.01);elsefloatVersion += ([[sysVersion objectAtIndex:1] floatValue] *0.10);}if (sysVersion.count > 2) {NSString* subVersion = [sysVersion objectAtIndex:2];if (subVersion.length == 1)floatVersion += ([[sysVersion objectAtIndex:2] floatValue] *0.0001);elsefloatVersion += ([[sysVersion objectAtIndex:2] floatValue] *0.0010);}
if (floatVersion >= targetVersion)return TRUE;
// elsereturn FALSE;}
// Get the system version of iOS at runtime.NSString *versionString = [[UIDevice currentDevice] systemVersion];
// Convert the version string to a Version instance.Version *version = [Version versionWithString:versionString];
// Dump the major, minor and micro version numbers.NSLog(@"version = [%d, %d, %d]",version.major, version.minor, version.micro);
// Check whether the version is greater than or equal to 6.0.if ([version isGreaterThanOrEqualToMajor:6 minor:0]){// The iOS version is greater than or equal to 6.0.}
// Another way to check whether iOS version is// greater than or equal to 6.0.if (6 <= version.major){// The iOS version is greater than or equal to 6.0.}
- (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;}
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)}
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)breakcase .OrderedAscending://current version is < (3.1.3)fallthroughdefault:break;}
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
+ (SInt64)integerFromVersionString:(NSString *)versionString withComponentCount:(NSUInteger)componentCount{//// performs base conversion from a version string to a decimal value. the version string is interpreted as// a base-10000 number, where each component is an individual digit. this makes it simple to use integer// operations for comparing versions. for example (with componentCount = 4)://// version "5.9.22.1" = 5*1000^3 + 9*1000^2 + 22*1000^1 + 1*1000^0 = 5000900220001// and// version "6.0.0.0" = 6*1000^3 + 0*1000^2 + 0*1000^1 + 0*1000^1 = 6000000000000// and// version "6" = 6*1000^3 + 0*1000^2 + 0*1000^1 + 0*1000^1 = 6000000000000//// then the integer comparisons hold true as you would expect://// "5.9.22.1" < "6.0.0.0" // true// "6.0.0.0" == "6" // true//
static NSCharacterSet *nonDecimalDigitCharacter;static dispatch_once_t onceToken;dispatch_once(&onceToken,^{ // don't allocate this charset every time the function is callednonDecimalDigitCharacter = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];});
SInt64 base = 10000; // each component in the version string must be less than baseSInt64 result = 0;SInt64 power = 0;
// construct the decimal value left-to-right from the version stringfor (NSString *component in [versionString componentsSeparatedByString:@"."]){if (NSNotFound != [component rangeOfCharacterFromSet:nonDecimalDigitCharacter].location){// one of the version components is not an integer, so bail outresult = -1;break;}result += [component longLongValue] * (long long)pow((double)base, (double)(componentCount - ++power));}
return result;}
+ (SInt64)integerFromVersionString:(NSString *)versionString{return [[self class] integerFromVersionString:versionStringwithComponentCount:[[versionString componentsSeparatedByString:@"."] count]];}
+ (SInt64)integerFromiOSVersionString:(NSString *)versionString{// iOS uses 3-component version stringreturn [[self class] integerFromVersionString:versionStringwithComponentCount:3];}
NSString *currentVersion = [[UIDevice currentDevice] systemVersion];if ([Util integerFromiOSVersionString:currentVersion] >= [Util integerFromiOSVersionString:@"42"]){NSLog(@"we are in some horrible distant future where iOS still exists");}