有一些方法在很久之前就有的,在高版本的系統(tǒng)上使用可能會有問題,此時(shí)就需要判斷用戶的系統(tǒng)版本來選擇執(zhí)行哪一種方法了.
- 例如
//在iOS8之前是使用這個(gè)方法獲取NSCalendar *的,在iOS8,iOS9上使用可能會有問題.
NSCalendar *calendar = [NSCalendar currentCalendar];
//在iOS8后,可以使用這個(gè)方法回去NSCalendar *
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
- 獲取當(dāng)前系統(tǒng)版本:
NSString *version = [UIDevice currentDevice].systemVersion;
- 第一種方法:通過系統(tǒng)版本號(1)
NSCalendar *calendar = nil;
if (version.doubleValue >= 8.0) { // iOS系統(tǒng)版本 >= 8.0
calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
} else{ //iOS系統(tǒng)版本 < 8.0
calendar = [NSCalendar currentCalendar];
}
- 第二種方法:通過系統(tǒng)版本號(2)
if ([version compare:@"8.0"] != NSOrderedAscending) { // iOS系統(tǒng)版本 >= 8.0,不等于升序,就是等于和降序
calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
}else{
calendar = [NSCalendar currentCalendar];
}
- (NSComparisonResult)compare:(NSString *)string;
這個(gè)方法是比較兩個(gè)字符串的大小,返回NSComparisonResult;
NSOrderedAscending 升序(右邊 > 左邊)
NSOrderedSame 相等、相同
NSOrderedDescending 降序(右邊 < 左邊)
例如:
8.91 compare 8.92000,首先會先比較第一位8和8比較,相同就繼續(xù)比第二位,9和9比較,相同,繼續(xù)比第三位1和2比較,2大,所以返回的結(jié)果是NSOrderedAscending;
-
第三種方法:通過比較Foundation框架的版本號
iOS系統(tǒng)升級的同時(shí)Foundation框架的版本也會提高,iPhone的iOS版本對應(yīng)的Foundation框架版本
#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
#endif
if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_8_0) { // iOS系統(tǒng)版本 >= 8.0
calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
}else
{
calendar = [NSCalendar currentCalendar];
}
- 第四種方法:通過特有的類進(jìn)行判斷
UIAlertController 這個(gè)類是iOS8之后才出現(xiàn)的NS_CLASS_AVAILABLE_IOS(8_0),如果當(dāng)前系統(tǒng)版本沒有這個(gè)類NSClassFromString(@"UIAlertController" == (null),從而判斷當(dāng)前版本是否大于等于iOS8
NSLog(@"%@,%@",NSClassFromString(@"UIAlertController"),NSClassFromString(@"ZBWPerson"));
打印結(jié)果:UIAlertController---UIAlertController,ZBWPerson---(null)
if (NSClassFromString(@"UIAlertController")) { // iOS系統(tǒng)版本 >= 8.0
}
- 第五種方法:通過方法進(jìn)行判斷
NS_AVAILABLE(10_11, 7_0):這個(gè)方法是iOS7之后才出現(xiàn)的
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context
if ([@"" respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) { // iOS系統(tǒng)版本 >= 7.0
}