做iOS版本適配的時(shí)候,因?yàn)橛行〢PI是在新的版本里才出現(xiàn)的,所以需要做版本的判斷。下面就說一下iOS提供的幾個(gè)版本判斷的方法
- OC: @available, Swift: #available
這個(gè)是xcode 9 新出的一個(gè)判斷iOS版本的方法, 下面的代碼是判斷scrollview在iOS 11中的新屬性的方法。
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
其中@available(iOS 11.0, *)第一個(gè)參數(shù)iOS 11.0代表在iOS 11.0版本以上,第二個(gè)參數(shù)*代表其他所有平臺(tái)。所以這段代碼的意思運(yùn)行這段代碼的系統(tǒng)是否在iOS 11.0和其他平臺(tái)上,如果是則執(zhí)行if里的語(yǔ)句,不是則執(zhí)行else里的語(yǔ)句。
- NSFoundationVersionNumber
NSFoundationVersionNumber是一個(gè)double值,定義在頭文件NSObjCRuntime.h中,這個(gè)頭文件中包含一系列的iOS、Mac的版本
FOUNDATION_EXPORT double NSFoundationVersionNumber;
#if TARGET_OS_MAC
#define NSFoundationVersionNumber10_0 397.40
#define NSFoundationVersionNumber10_1 425.00
#define NSFoundationVersionNumber10_1_1 425.00
#define NSFoundationVersionNumber10_1_2 425.00
#define NSFoundationVersionNumber10_1_3 425.00
#define NSFoundationVersionNumber10_1_4 425.00
#define NSFoundationVersionNumber10_2 462.00
#define NSFoundationVersionNumber10_2_1 462.00
#define NSFoundationVersionNumber10_2_2 462.00
#define NSFoundationVersionNumber10_2_3 462.00
#define NSFoundationVersionNumber10_2_4 462.00
#define NSFoundationVersionNumber10_2_5 462.00
#define NSFoundationVersionNumber10_2_6 462.00
#define NSFoundationVersionNumber10_2_7 462.70
#define NSFoundationVersionNumber10_2_8 462.70
#define NSFoundationVersionNumber10_3 500.00
#define NSFoundationVersionNumber10_3_1 500.00
#define NSFoundationVersionNumber10_3_2 500.30
#define NSFoundationVersionNumber10_3_3 500.54
#define NSFoundationVersionNumber10_3_4 500.56
#define NSFoundationVersionNumber10_3_5 500.56
#define NSFoundationVersionNumber10_3_6 500.56
#define NSFoundationVersionNumber10_3_7 500.56
#define NSFoundationVersionNumber10_3_8 500.56
#define NSFoundationVersionNumber10_3_9 500.58
#define NSFoundationVersionNumber10_4 567.00
#define NSFoundationVersionNumber10_4_1 567.00
#define NSFoundationVersionNumber10_4_2 567.12
#define NSFoundationVersionNumber10_4_3 567.21
#define NSFoundationVersionNumber10_4_4_Intel 567.23
#define NSFoundationVersionNumber10_4_4_PowerPC 567.21
#define NSFoundationVersionNumber10_4_5 567.25
#define NSFoundationVersionNumber10_4_6 567.26
#define NSFoundationVersionNumber10_4_7 567.27
#define NSFoundationVersionNumber10_4_8 567.28
#define NSFoundationVersionNumber10_4_9 567.29
#define NSFoundationVersionNumber10_4_10 567.29
#define NSFoundationVersionNumber10_4_11 567.36
#define NSFoundationVersionNumber10_5 677.00
#define NSFoundationVersionNumber10_5_1 677.10
#define NSFoundationVersionNumber10_5_2 677.15
#define NSFoundationVersionNumber10_5_3 677.19
#define NSFoundationVersionNumber10_5_4 677.19
#define NSFoundationVersionNumber10_5_5 677.21
#define NSFoundationVersionNumber10_5_6 677.22
#define NSFoundationVersionNumber10_5_7 677.24
#define NSFoundationVersionNumber10_5_8 677.26
#define NSFoundationVersionNumber10_6 751.00
#define NSFoundationVersionNumber10_6_1 751.00
#define NSFoundationVersionNumber10_6_2 751.14
#define NSFoundationVersionNumber10_6_3 751.21
#define NSFoundationVersionNumber10_6_4 751.29
#define NSFoundationVersionNumber10_6_5 751.42
#define NSFoundationVersionNumber10_6_6 751.53
#define NSFoundationVersionNumber10_6_7 751.53
#define NSFoundationVersionNumber10_6_8 751.62
#define NSFoundationVersionNumber10_7 833.10
#define NSFoundationVersionNumber10_7_1 833.10
#define NSFoundationVersionNumber10_7_2 833.20
#define NSFoundationVersionNumber10_7_3 833.24
#define NSFoundationVersionNumber10_7_4 833.25
#define NSFoundationVersionNumber10_8 945.00
#define NSFoundationVersionNumber10_8_1 945.00
#define NSFoundationVersionNumber10_8_2 945.11
#define NSFoundationVersionNumber10_8_3 945.16
#define NSFoundationVersionNumber10_8_4 945.18
#define NSFoundationVersionNumber10_9 1056
#define NSFoundationVersionNumber10_9_1 1056
#define NSFoundationVersionNumber10_9_2 1056.13
#define NSFoundationVersionNumber10_10 1151.16
#define NSFoundationVersionNumber10_10_1 1151.16
#define NSFoundationVersionNumber10_10_2 1152.14
#define NSFoundationVersionNumber10_10_3 1153.20
#define NSFoundationVersionNumber10_10_4 1153.20
#define NSFoundationVersionNumber10_10_5 1154
#define NSFoundationVersionNumber10_10_Max 1199
#define NSFoundationVersionNumber10_11 1252
#define NSFoundationVersionNumber10_11_1 1255.1
#define NSFoundationVersionNumber10_11_2 1256.1
#define NSFoundationVersionNumber10_11_3 1256.1
#define NSFoundationVersionNumber10_11_4 1258
#define NSFoundationVersionNumber10_11_Max 1299
#endif
#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
如果要判斷系統(tǒng)運(yùn)行的版本可以使用下面代碼
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_9_x_Max) {
} else {
}
不過這個(gè)頭文件里定義的最大的宏是NSFoundationVersionNumber_iOS_9_x_Max。不過NSFoundationVersionNumber在iOS 10.3.1上和iOS 11上輸出的值分別為1349.55和1444.12,看來蘋果只是沒有提供對(duì)應(yīng)的宏,但是卻改變了NSFoundationVersionNumber的值。
- UIDevice 的systemVersion屬性
可以使用如下代碼判斷系統(tǒng)版本,這段代碼使用xcode 9在iOS 10.3.1模擬器上運(yùn)行
NSString *version = [UIDevice currentDevice].systemVersion;
float float_version = [version floatValue]; // 10.300000
NSLog(@"%f", float_version);
if (float_version >= 10.3) {
NSLog(@"系統(tǒng)版本大于等于10.3");
} else {
NSLog(@"系統(tǒng)版本小于10.3");
}
// 輸出 系統(tǒng)版本大于等于10.3
- 判斷某個(gè)系統(tǒng)版本的方法或類來判斷系統(tǒng)版本
if (NSStringFromClass([UIAlertController class])) {
NSLog(@"iOS 8 以上");
}
if ([@"" respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) { // 存在這個(gè)方法
NSLog(@"iOS 7 以上");
}
- __IPHONE_OS_VERSION_MIN_REQUIRED, __IPHONE_OS_VERSION_MAX_ALLOWED,
__IPHONE_11_0
這三個(gè)宏個(gè)人感覺是比較坑的,剛開始看這個(gè)宏感覺就是判斷iOS的最大使用版本、最小版本的,可是用上才發(fā)現(xiàn)完全是錯(cuò)的。
這三個(gè)宏是在編譯期間就已經(jīng)確定好的,__IPHONE_OS_VERSION_MIN_REQUIRED這個(gè)宏是你在xcode中設(shè)置的Develoyment target,也就是你的app能夠運(yùn)行的最小的iOS版本。__IPHONE_OS_VERSION_MAX_ALLOWED這個(gè)宏就是你的xcode支持的最大iOS版本,__IPHONE_11_0這個(gè)宏也是跟xcode版本有關(guān)。所以這三個(gè)宏都是在編譯期間就已經(jīng)確定好了的,所以不能用來動(dòng)態(tài)判斷你的app運(yùn)行在哪個(gè)系統(tǒng)版本的iOS上。這一點(diǎn)大家一定要弄清,否則就容易被坑。
以上就是我所了解到的判斷系統(tǒng)版本的內(nèi)容,有什么不對(duì)的地方歡迎大家指出。
參考鏈接: