由于系统平台和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. What are DESC and ASC Keywords?

    What are DESC and ASC Keywords? ASC is the short form for ascending DESC is the short form for desce ...

  2. MySQL中备份的几种方式

    前言: 并不是每家公司都高大上,并不是每家公司都会用一些很前沿的技术来做备份这一块,有些企业或者有些行业或者团队本身由于各方面的原因使用简单或者复杂的方式来做备份这块,这次这个文档算是对以前工作的总结 ...

  3. Linux下安装MongoDB全程记录

    1.下载安装包 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.6.0.tgz 2.解压缩 [root@loca ...

  4. 斯坦福大学Andrew Ng - 机器学习笔记(2) -- 逻辑回归 & 正则化

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

  5. 卸载SQL Server 2008 (R2)

    一.卸载SQL Server 2008 (R2) 1.找到控制面板,win8及win7都可以直接点解“开始”按钮找到. (Tip:win10系统的小盆友可以在“开始”菜单下点击“所有应用”,找到win ...

  6. PyQt4 进度条和日历 代码

    # -*- coding: utf-8 -*- """ ------------------------------------------------- File Na ...

  7. Java FAQ -- "Exception in thread 'main' java.lang.UnsupportedClassVersionError:"

    OS:Ubuntu 最近重新学习Java,写了一段很小的程序,如下: public class Hello{ public static void main(String args[]){ Syste ...

  8. C#对Excel中指定一列或一行实现隐藏或显示!

    C#对Excel中指定一列或一行实现隐藏或显示!不会,求指导!

  9. Eclipse 变量高亮显示设置

    A:Window-> preferences->java->Editor->Mark Occurences Local variables就是变量的高亮显示

  10. c# 接口(interface)与接口应用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; //接口(interface ...