[iOS]適配iOS10問題

weixin_34357887發表於2016-09-14

1.系統判斷方法失效:

在你的專案中,當需要判斷系統版本的話,不要使用下面的方法:
#define isiOS10 ([[[[UIDevice currentDevice] systemVersion]   substringToIndex:1] intValue]>=10)

它會永遠返回NO,substringToIndex:1,在iOS 10 會被檢測成 iOS 1了。
Objective-C:
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
或者使用:
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ .majorVersion = 9, .minorVersion = 1, .patchVersion = 0}]) { 
    NSLog(@"Hello from > iOS 9.1");
}
if ([NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){9,3,0}]) { 
    NSLog(@"Hello from > iOS 9.3");
}
也可以使用:
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_9_0) { // do stuff for iOS 9 and newer} else { 
    // do stuff for older versions than iOS 9
}
有時候會缺少一些常量,NSFoundationVersionNumber是在NSObjCRuntime.h中定義的,作為Xcode7.3.1的一部分,我們設定常熟範圍從iPhone OS 2到#define NSFoundationVersionNumber_iOS_8_4 1144.17,在iOS 10(Xcode 8)中,蘋果補充了缺少的數字,設定有未來的版本.
#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

Swift:
if NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: 10, minorVersion: 0, patchVersion: 0)) { 
    // 程式碼塊
}
或者:
if #available(iOS 10.0, *) {  
    // 程式碼塊
} else { 
    // 程式碼塊
}

相關文章