由于系统平台和SDK更新迭代,一部分过时的成员、方法会被彻底从SDK中移除,为了兼容旧的设备,这时就需要区分系统平台版本调用正确的API。

另一种情况是iOS设备的屏幕和设备参数不同,虽然UI上的AutoLayout技术能很好的解决显示适配问题,但仍有时候不得不根据平台版本进行编码。

有很多方法来区分平台版本,常用的有 [UIDevice currentDevice].systemVersion floatValue] ,或者根据SDK更新增加和淘汰的Class进行判断(不直观且必须对历史版本非常了解),根据设备情况判断(最不可取),通过NSObjCRuntime.h的宏定义判断(使用NSFoundationVersionNumber)。

最建议这种方法,因为可读性较高,支持iOS2以后的所有及可预见的未来版本。

举个例子:CFURLCreateStringByReplacingPercentEscapesUsingEncoding在iOS9中被完全移除,为了同时兼容iOS6版本,decodedUrlString代码如下:

 #ifndef NSFoundationVersionNumber_iOS_7_0
#define NSFoundationVersionNumber_iOS_7_0 1047.20
#endif -(NSString *)decodedUrlString:(NSString *)urlString {
if (NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_7_0) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
NSString * decodedString = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (__bridge CFStringRef)urlString, CFSTR(""), kCFStringEncodingUTF8);
#pragma clang diagnostic pop
return decodedString;
} else {
return [urlString stringByRemovingPercentEncoding];
}
}

解释:因为iOS8之前的SDK未定义NSFoundationVersionNumber_iOS_7_0,而iOS7之后可以使用stringByRemovingPercentEncoding方法代替前面的方法,这里给没有定义NSFoundationVersionNumber_iOS_7_0的情况手动定义它,使编译器能编译旧版本SDK。它的值见NSObjCRuntime.h中的定义,可用版本见开发文档:

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
#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
#endif

回到代码第6行,对版本进行比较,这里必须是运行时代码,如果是iOS7之前就调用旧的方法,否则使用iOS7之后的替代方案。

第8行是忽略使用了已销毁定义的编译警告。

其他的方案讨论,请看.so中的讨论:http://stackoverflow.com/questions/3339722/how-to-check-ios-version/

iOS开发中的系统版本比较的更多相关文章

  1. iOS开发之判断系统版本

    if([[UIDevice currentDevice].systemVersion doubleValue]>=7.0) { //是IOS7至以上版本 }else{ //IOS7以下版本 }

  2. GIT在iOS开发中的使用

    前言 在iOS开发中,很多公司对项目的版本控制管理都使用了git,当然也有部分公司使用的是svn.当年我最初接触的是svn,觉得使用起来挺方便的,但是每次切分支都需要下载一份新的代码起来,这实在太麻烦 ...

  3. iOS开发中的4种数据持久化方式【一、属性列表与归档解档】

    iOS中的永久存储,也就是在关机重新启动设备,或者关闭应用时,不会丢失数据.在实际开发应用时,往往需要持久存储数据的,这样用户才能在对应用进行操作后,再次启动能看到自己更改的结果与痕迹.ios开发中, ...

  4. 在iOS开发中,给项目添加新的.framework

    首先需要了解一下iOS中静态库和动态库.framework的概念 静态库与动态库的区别 首先来看什么是库,库(Library)说白了就是一段编译好的二进制代码,加上头文件就可以供别人使用. 什么时候我 ...

  5. iOS开发中你是否遇到这些经验问题

    前言 小伙伴们在开发中难免会遇到问题, 你是如何解决问题的?不妨也分享给大家!如果此文章其中的任何一条问题对大家有帮助,那么它的存在是有意义的! 反正不管怎样遇到问题就要去解决问题, 在解决问题的同时 ...

  6. 深入理解 iOS 开发中的锁

    来源:伯乐在线 - 夏天然后 链接:http://ios.jobbole.com/89474/ 点击 → 申请加入伯乐在线专栏作者 摘要 本文的目的不是介绍 iOS 中各种锁如何使用,一方面笔者没有大 ...

  7. iOS开发中遇到的一些问题及解决方案【转载】

    iOS开发中遇到的一些问题及解决方案[转载] 2015-12-29 [385][scrollView不接受点击事件,是因为事件传递失败] // //  MyScrollView.m //  Creat ...

  8. iOS开发中断言的使用—NSAssert()

    原文链接:http://blog.csdn.net/univcore/article/details/16859263 断言(assertion)是指在开发期间使用的.让程序在运行时进行自检的代码(通 ...

  9. 总结iOS开发中的断点续传那些事儿

    前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...

随机推荐

  1. 斯坦福大学Andrew Ng - 机器学习笔记(1) -- 单变量&多变量线性回归

    大概用了一个月,Andrew Ng老师的机器学习视频断断续续看完了,以下是个人学习笔记,入门级别,权当总结.笔记难免有遗漏和误解,欢迎讨论. 鸣谢:中国海洋大学黄海广博士提供课程视频和个人笔记,在此深 ...

  2. MySql 自适应哈希索引

    一.介绍 哈希(hash)是一种非常快的查找方法,一般情况下查找的时间复杂度为O(1).常用于连接(join)操作,如Oracle中的哈希连接(hash join). InnoDB存储引擎会监控对表上 ...

  3. slf4j和log4j结合使用步骤

    使用slf4j的优点: 提供带参数的日志输出方法(SLF4J 1.7及以后版本). pom中只需引入slf4j-log4j12,然后maven会引入它所依赖的其它JAR包. slf4j和log4j结合 ...

  4. PAT 天梯赛 L1-030. 一帮一 【STL】

    题目链接 https://www.patest.cn/contests/gplt/L1-030 思路 用三个 Vector 来分别存放 整个排名,以及男生的单独排名和女生的单独排名 从整个的排名 从上 ...

  5. Fidder详解之抓包

    前言 本文是博主发表的第一篇文章,如有傻逼之处,请大家见谅.最近遇到很多人说接口相关的问题,比如:什么是接口,我该怎么做接口测试,还有我总是抓不到APP上的https请求(这个巨坑,不知道坑了多少小白 ...

  6. android 加固防止反编译-重新打包

    http://blog.csdn.net/u010921385/article/details/52505094 1.需要加密的Apk(源Apk) 2.壳程序Apk(负责解密Apk工作) 3.加密工具 ...

  7. LVS/NAT 配置

    LVS/NAT 配置 实验环境 三台主机:Linux Centos 6.4 32位 调度器Director:192.168.1.160(内网IP).192.168.2.20(公网IP) HTTP真实服 ...

  8. 写给后端程序员的HTTP缓存原理介绍--怎样决定一个资源的Cache-Control策略呢

    通过Internet获取资源既缓慢,成本又高.为此,Http协议里包含了控制缓存的部分,以使Http客户端可以缓存和重用以前获 取的资源,从而优化性能,提升体验.虽然Http中关于缓存控制的部分,随着 ...

  9. 《网络对抗》 逆向及Bof基础实践

    <网络对抗>-逆向及Bof基础实践 1 逆向及Bof基础实践说明 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数, ...

  10. Java基础面试集合

    1.面向对象的特征有哪些方面? 抽象 封装 继承 多态,多态性是指允许不同子类型的对象对同一消息作出不同的响应.简单的说就是用同样的对象引用调用同样的方法但是做了不同的事情.多态性分为编译时的多态性和 ...