iOS版本判断

做iOS版本适配的时候,因为有些API是在新的版本里才出现的,所以需要做版本的判断。下面就说一下iOS提供的几个版本判断的方法

  1. OC: @available, Swift: #available
    这个是xcode 9 新出的一个判断iOS版本的方法, 下面的代码是判断scrollview在iOS 11中的新属性的方法。
  if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

其中@available(iOS 11.0, *)第一个参数iOS 11.0代表在iOS 11.0版本以上,第二个参数*代表其他所有平台。所以这段代码的意思运行这段代码的系统是否在iOS 11.0和其他平台上,如果是则执行if里的语句,不是则执行else里的语句。

  1. NSFoundationVersionNumber
    NSFoundationVersionNumber是一个double值,定义在头文件NSObjCRuntime.h中,这个头文件中包含一系列的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

如果要判断系统运行的版本可以使用下面代码

   if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_9_x_Max) {
        
    } else {
        
    }

不过这个头文件里定义的最大的宏是NSFoundationVersionNumber_iOS_9_x_Max。不过NSFoundationVersionNumber在iOS 10.3.1上和iOS 11上输出的值分别为1349.55和1444.12,看来苹果只是没有提供对应的宏,但是却改变了NSFoundationVersionNumber的值。

  1. UIDevice 的systemVersion属性
    可以使用如下代码判断系统版本,这段代码使用xcode 9在iOS 10.3.1模拟器上运行
    NSString *version = [UIDevice currentDevice].systemVersion;
    float float_version = [version floatValue]; // 10.300000
    NSLog(@"%f", float_version);
    if (float_version >= 10.3) {
        NSLog(@"系统版本大于等于10.3");
    } else {
        NSLog(@"系统版本小于10.3");
    }
// 输出 系统版本大于等于10.3
  1. 判断某个系统版本的方法或类来判断系统版本
  if (NSStringFromClass([UIAlertController class])) {
        NSLog(@"iOS 8 以上");
    }
    if ([@"" respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) { // 存在这个方法
        NSLog(@"iOS 7 以上");
    }
  1. __IPHONE_OS_VERSION_MIN_REQUIRED, __IPHONE_OS_VERSION_MAX_ALLOWED,
    __IPHONE_11_0
    这三个宏个人感觉是比较坑的,刚开始看这个宏感觉就是判断iOS的最大使用版本、最小版本的,可是用上才发现完全是错的。
    这三个宏是在编译期间就已经确定好的,__IPHONE_OS_VERSION_MIN_REQUIRED这个宏是你在xcode中设置的Develoyment target,也就是你的app能够运行的最小的iOS版本。__IPHONE_OS_VERSION_MAX_ALLOWED这个宏就是你的xcode支持的最大iOS版本,__IPHONE_11_0这个宏也是跟xcode版本有关。所以这三个宏都是在编译期间就已经确定好了的,所以不能用来动态判断你的app运行在哪个系统版本的iOS上。这一点大家一定要弄清,否则就容易被坑。
    以上就是我所了解到的判断系统版本的内容,有什么不对的地方欢迎大家指出。

参考链接:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 26,388评论 7 249
  • 街头喂奶,不是什么新鲜事,想必每个荣升为母亲的女人多多少少都经历过,新鲜就新鲜在大众对此事的认知,相信绝大多数女性...
    文案妞阅读 940评论 0 1
  • 人在年少时太过脆嫩 却不甘就此陷入慈温 于是出奔 在承星履草的春耕 在雨恨云愁的清晨 每次跌跌撞撞尝到疼 总是不听...
    夏小溪_阅读 493评论 2 5
  • 和我去流浪吧,在火车上看书,川端康成或者村上春树;听和谐号的铁路广播和八十年代的老歌;途径西安的日出,郑州的日落,...
    面面小朋友阅读 349评论 0 0
  • 小咖秀的title是「人生如戏全凭演技」,这一技实在让人始料未及啊... 最近最热门的App当属小咖秀莫属了,7月...
    ElekChen阅读 1,196评论 3 5

友情链接更多精彩内容