1柿扣、UIDevice方式
使用 [[UIDevice currentDevice] systemVersion]
來判斷
(1)比較字符串
- (void)test1 {
NSString *systemVersion = [[UIDevice currentDevice] systemVersion
];
NSLog(@"%@"
,systemVersion);
// 以前比較的時(shí)候可以直接截取第一個(gè)字符進(jìn)行比較,但在 iOS10 以后瓣赂,如果截取第一個(gè)字符李命,只能得到1
// 所以最好的方法就是用整個(gè)字符串直接比較
if ([systemVersion compare:@"9.3.5" options:NSNumericSearch] == NSOrderedDescending) {
NSLog(@“大于 iOS 9.3.5");
}
}
(2)轉(zhuǎn)化成 float 進(jìn)行比較,只能比較前面的 majorVersion 和 minorVersion
- (void)test2 {
// 轉(zhuǎn)化成 float 進(jìn)行比較
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
// version float value = 10.300000
NSLog(@"version float value = %f",systemVersion.floatValue);
if (systemVersion.floatValue > 10.0) {
NSLog(@"當(dāng)前系統(tǒng)版本大于 10.0");
} else {
NSLog(@"當(dāng)前系統(tǒng)版本小于 10.0");
}
}
2蕉汪、使用系統(tǒng)定義的 宏 來判斷
- (void)test3 {
// NSFoundationVersionNumber 是一個(gè)宏流译,double 類型,代表當(dāng)前系統(tǒng)的版本
// NSFoundationVersionNumber_iOS_9_3 是系統(tǒng)定義的宏者疤,也是 double 類型福澡,代表 iOS 9.3.x 的系統(tǒng)版本
double currentFoundationVersion = NSFoundationVersionNumber;
NSLog(@"currentFoundationNumber = %f",currentFoundationVersion);
if (currentFoundationVersion > NSFoundationVersionNumber_iOS_9_3) {
NSLog(@"大于 iOS 9.3.0");
}
}
系統(tǒng)中的宏定義與當(dāng)前使用的SDK有關(guān),我當(dāng)前的使用的 Xcode 是 8.3.2
#if TARGET_OS_IPHONE
#define NSFoundationVersionNumber_iPhoneOS_2_0 678.24
#define NSFoundationVersionNumber_iPhoneOS_2_1 678.26
#define NSFoundationVersionNumber_iPhoneOS_2_2 678.29
#define NSFoundationVersionNumber_iPhoneOS_3_0 678.47
#define NSFoundationVersionNumber_iPhoneOS_3_1 678.51
#define NSFoundationVersionNumber_iPhoneOS_3_2 678.60
#define NSFoundationVersionNumber_iOS_4_0 751.32
#define NSFoundationVersionNumber_iOS_4_1 751.37
#define NSFoundationVersionNumber_iOS_4_2 751.49
#define NSFoundationVersionNumber_iOS_4_3 751.49
#define NSFoundationVersionNumber_iOS_5_0 881.00
#define NSFoundationVersionNumber_iOS_5_1 890.10
#define NSFoundationVersionNumber_iOS_6_0 992.00
#define NSFoundationVersionNumber_iOS_6_1 993.00
#define NSFoundationVersionNumber_iOS_7_0 1047.20
#define NSFoundationVersionNumber_iOS_7_1 1047.25
#define NSFoundationVersionNumber_iOS_8_0 1140.11
#define NSFoundationVersionNumber_iOS_8_1 1141.1
#define NSFoundationVersionNumber_iOS_8_2 1142.14
#define NSFoundationVersionNumber_iOS_8_3 1144.17
#define NSFoundationVersionNumber_iOS_8_4 1144.17
#define NSFoundationVersionNumber_iOS_8_x_Max 1199
#define NSFoundationVersionNumber_iOS_9_0 1240.1
#define NSFoundationVersionNumber_iOS_9_1 1241.14
#define NSFoundationVersionNumber_iOS_9_2 1242.12
#define NSFoundationVersionNumber_iOS_9_3 1242.12
#define NSFoundationVersionNumber_iOS_9_4 1280.25
#define NSFoundationVersionNumber_iOS_9_x_Max 1299
#endif
3驹马、使用 NSProcessInfo 來判斷
- (void)test4 {
// operatingSystemVersionString = Version 10.3.1 (Build 14E304)
NSLog(@"operatingSystemVersionString = %@",[[NSProcessInfo processInfo] operatingSystemVersionString]);
// operatingSystemVersion 是一個(gè)結(jié)構(gòu)體革砸,表示當(dāng)前的系統(tǒng)版本 X.X.X
/*
typedef struct {
NSInteger majorVersion;
NSInteger minorVersion;
NSInteger patchVersion;
} NSOperatingSystemVersion;
*/
NSLog(@"operatingSystemVersion = %ld",[[NSProcessInfo processInfo] operatingSystemVersion].majorVersion);
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){10,3,5}]) {
NSLog(@"大于 iOS 10.3.5");
} else {
NSLog(@"小于 iOS 10.3.5");
}
}
4、Switft 中用法
在 Swift 中支持內(nèi)置版本檢查的語言糯累,非常簡單
func test4() {
if #available(iOS 10.0.1, *) {
print("iOS 10.0.1 is available")
} else {
print("iOS 10.0.1 is not available")
}
// 當(dāng)前系統(tǒng)大于等于 9.3.5 時(shí)算利,返回 true
if #available(iOS 9.3.5, *) {
print("iOS 9.3.5 is available")
}
}