美文网首页iOS-行知iOS 收集程序员
判断系统版本的方法(用于方法的系统适配)

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

作者: Callmewenxi | 来源:发表于2016-04-21 22:47 被阅读16335次

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

  • 例如
//在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,首先会先比较第一位8和8比较,相同就继续比第二位,9和9比较,相同,继续比第三位1和2比较,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
        
    }

相关文章

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

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

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

    判断系统版本的方法(用于方法的系统适配) 字数658阅读597评论1喜欢5 有一些方法在很久之前就有的,在高版本的...

  • iOS10和Xcode8适配整理

    *摘抄于网络各路大神 — — * 一、iOS10 适配问题 1. 系统判断版本方法 2. UserNotific...

  • iOS判断系统版本号的方法

    很多系统方法都有版本支持的说明,所以对于支持多个系统版本,需要判断系统版本来执行方法。比如: 第一个方法只支持7-...

  • iOS 中 有意思的事情

    判断系统的版本代码: 枚举 在某些版本中替换掉系统的方法 交换两个类方法 替换类中的方法 OC中大写自动分割代码 ...

  • 项目总结-9

    方法的系统适配 以日历类为例:NSCalendar对象的创建有几种方法,但是那些新的方法是支持高版本系统的,也就是...

  • iOS10 适配总结

    先整理一下iOS10适配内容 1、iOS 10 版本适配问题 1.系统判断方法失效2.隐私数据访问问题3.UICo...

  • 判断iOS操作系统的几种方法

    方法一: 方法二: 方法三: 常用的是方法二和方法三 参考文章:【iOS 10 判断系统版本方式更新】

  • iOS 13 黑暗模式适配

    1. 颜色适配 2.图片适配 又新增一个UITraitCollection方法,我们可以通过它来判断当前系统的模式...

  • 方法的系统适配

    在实际的开发过程中,为了避免在不同的系统版本中报找不到方法的错误从而导致系统崩溃,我们常常需要对不同的版本进行适配...

网友评论

  • 西叶lv:@Available(iOS 11.0, *)
  • fuyoufang:在『第二种方法:通过系统版本号(2) 』中,if ([version compare:@"8.0"] != NSOrderedAscending) 的判断方法不对,因为如果当系统为10.0.0 和 目标系统 8.0.0 比较的时候,结果是错误的。应该为 : if ([version compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending)
    5dbca6037233:@fuyoufang 的确会坑啊, 还是转换成 nsnumber,再比较靠谱点

本文标题:判断系统版本的方法(用于方法的系统适配)

本文链接:https://www.haomeiwen.com/subject/lxatrttx.html