系统版本判断(一)

各版本的兼容问题:http://git.devzeng.com/blog/ios-multiple-version-compatible.html

判断系统版本的方法(用于方法的系统适配)

有一些方法在很久之前就有的,在高版本的系统上使用可能会有问题,此时就需要判断用户的系统版本来选择执行哪一种方法了.

  • 例如
//在iOS8之前是使用这个方法获取NSCalendar *的,在iOS8,iOS9上使用可能会有问题.
NSCalendar *calendar = [NSCalendar currentCalendar]; 

//在iOS8后,可以使用这个方法回去NSCalendar *
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
  • 获取当前系统版本:
NSString *version = [UIDevice currentDevice].systemVersion;
  • 第一种方法:通过系统版本号(1)
NSCalendar *calendar = nil;
if (version.doubleValue >= 8.0) { // iOS系统版本 >= 8.0
   calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
} else{ //iOS系统版本 < 8.0
   calendar = [NSCalendar currentCalendar]; 
}
  • 第二种方法:通过系统版本号(2)
if ([version compare:@"8.0"] != NSOrderedAscending) { // iOS系统版本 >= 8.0,不等于升序,就是等于和降序
   calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
}else{
   calendar = [NSCalendar currentCalendar]; 
    }
- (NSComparisonResult)compare:(NSString *)string;
这个方法是比较两个字符串的大小,返回NSComparisonResult;

NSOrderedAscending 升序(右边 > 左边)
NSOrderedSame 相等、相同
NSOrderedDescending 降序(右边 < 左边)

例如:
8.91 compare 8.92000,首先会先比较第一位88比较,相同就继续比第二位,99比较,相同,继续比第三位12比较,2大,所以返回的结果是NSOrderedAscending;
  • 第三种方法:通过比较Foundation框架的版本号
    iOS系统升级的同时Foundation框架的版本也会提高,

    iPhone的iOS版本对应的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系统版本 >= 8.0
    calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
}else
{
   calendar = [NSCalendar currentCalendar];  
}
  • 第四种方法:通过特有的类进行判断
    UIAlertController 这个类是iOS8之后才出现的NS_CLASS_AVAILABLE_IOS(8_0),如果当前系统版本没有这个类NSClassFromString(@"UIAlertController" == (null),从而判断当前版本是否大于等于iOS8
 NSLog(@"%@,%@",NSClassFromString(@"UIAlertController"),NSClassFromString(@"ZBWPerson"));

 打印结果:UIAlertController---UIAlertController,ZBWPerson---(null)
if (NSClassFromString(@"UIAlertController")) { // iOS系统版本 >= 8.0

    }
  • 第五种方法:通过方法进行判断
NS_AVAILABLE(10_11, 7_0):这个方法是iOS7之后才出现的

- (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系统版本 >= 7.0

    }

(int)([[[UIDevice currentDevice] systemVersion] floatValue])

将系统版本号转换成整型
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值