iOS多版本多设备适配的问题
2014-07-10 17:55
232 查看
好吧,能找到这文章的,一般是接到了如下需求:
![](http://cdn2.raywenderlich.com/wp-content/uploads/2013/06/alltheversions-e1371759958187.png)
我是从raywenderlich抽了点内容出来做日记,另外,本文说的不是布局的适配,而是因为ios的升级带来的各版本代码上的不兼容。
而Deployment Target则表示了你愿意支持的最低版本,位于同样的
![](http://cdn3.raywenderlich.com/wp-content/uploads/2013/06/RWRageFaces-7.png)
Sometimes the best documentation of methods and classes is contained right in the source code.
Simply Command-click the symbol you want to inspect, and Xcode will take you to its header file, where you will find the information you need along with the parameters of the preprocessor macros NS_AVAILABLE_IOS() and NS_CLASS_AVAILABLE_IOS(). If a class or method declaration doesn’t use either of those availability macros, you can assume that they won’t cause any compatibility issues.
Note: ALT + click on a method will display a popover that contains these same details along with other information about the method.
https://developer.apple.com/library/ios/releasenotes/General/iOS60APIDiffs/
https://developer.apple.com/library/ios/releasenotes/General/iOS70APIDiffs/
依次类推,
当然预览状态的就是如下了:
https://developer.apple.com/library/prerelease/ios/releasenotes/General/iOS80APIDiffs/
如果是类方法,用类而不是实例即可:
同样的方法可以探测类是否支持某个属性,比如UILabel是否有
常量也一样:
Checking for the existence of enumeration or bit mask values — the kind that you would find inside an NS_ENUM or NS_OPTIONS declaration — is incredibly difficult to check at runtime. Why?
Under the hood, an enumeration is just a method of giving names to numeric constants. An enum is replaced with an int when compiled, which always exists!
If you’re faced with the situation of needing to see if a certain enumeration exists, all you can do is either explicitly check the OS version (which isn’t recommended) or alternatively check against another API element that was introduced at the same time as the new enumeration value.
Note: Whichever way you do this, be sure to add adequate comments to any such code and consider wrapping the code in a dedicated compatibility helper.
最后,水果官方的兼容性指南:SDK Compatibility Guide
![](http://cdn2.raywenderlich.com/wp-content/uploads/2013/06/alltheversions-e1371759958187.png)
我是从raywenderlich抽了点内容出来做日记,另外,本文说的不是布局的适配,而是因为ios的升级带来的各版本代码上的不兼容。
Deployment Target vs. Base SDK
总的来说,Base SDK表示你愿意支持的最高版本,位于你要设置的Target的属性页的Build Settings > Architectures,一般就选择Latest iOS即可,比如我写这篇日志的时候已经是8.0了
而Deployment Target则表示了你愿意支持的最低版本,位于同样的
Build Setting下的
Deployment一节里面,你可以找到
iOS Deployment Target
发现兼容问题
这个简单:运行老旧系统的真机
你有那么多机器的话运行模拟器
你有装那么多SDK的话,当然这个也不是不可能啦苹果的文档
![](http://cdn3.raywenderlich.com/wp-content/uploads/2013/06/RWRageFaces-7.png)
Header Files
原文如下,但是我真没发现Command-click后跳转的有 NS_AVAILABLE_IOS() and NS_CLASS_AVAILABLE_IOS()这两个宏,这不能说明就没有兼容性问题吧?。。。Sometimes the best documentation of methods and classes is contained right in the source code.
Simply Command-click the symbol you want to inspect, and Xcode will take you to its header file, where you will find the information you need along with the parameters of the preprocessor macros NS_AVAILABLE_IOS() and NS_CLASS_AVAILABLE_IOS(). If a class or method declaration doesn’t use either of those availability macros, you can assume that they won’t cause any compatibility issues.
Note: ALT + click on a method will display a popover that contains these same details along with other information about the method.
苹果提供的Apid Diffs
这个应该是最全的了https://developer.apple.com/library/ios/releasenotes/General/iOS60APIDiffs/
https://developer.apple.com/library/ios/releasenotes/General/iOS70APIDiffs/
依次类推,
当然预览状态的就是如下了:
https://developer.apple.com/library/prerelease/ios/releasenotes/General/iOS80APIDiffs/
Deploymate
这是一个付费服务,简单来说,花钱外包了。油条帮上的演示视频deploymate,自行FQMJGAvailability
一个示例头文件,来自MJGFoundation,会欺骗编译器某些api已经deprecated了,从而触发警告,用法在文件头已经说得很清楚了,自己可以试试。解决兼容问题
新版本会引入新的框架、类、方法、常量以及枚举,分别如下frameworks
在Build Phases -> Link Binary With Libraries,将特定框架从
Required改为
Optional,这将会weak link你选定的框架
classes
目标版本可能不支持的类,文章提供的是iOS4.2的写法,严重怀疑现在有了新的写法,或者已经不支持了,希望有人留个言:if ([SLComposeViewController class]) { //Safe to use SLComposeViewController } else { //Fail gracefully }
methods
if ([self.image respondsToSelector:@selector(resizableImageWithCapInsets:resizingMode:)]) { //Safe to use this way of creating resizable images } else { //Fail gracefully }
如果是类方法,用类而不是实例即可:
if ([UIView respondsToSelector:@selector(requiresConstraintBasedLayout)]) { //Safe to use this method } else { //Fail gracefully }
同样的方法可以探测类是否支持某个属性,比如UILabel是否有
attributedText,可以用
@selector(setAttributedText)
constants/C functions
一般表现为extern NSString *或C语言的方法,比如一个iOS6引入的c方法
ABAddressBookCreateWithOptions(...),在iOS5中想不挂掉可以这么判断:
if (ABAddressBookCreateWithOptions != NULL) { //Safe to use } else { //Fail gracefully }
常量也一样:
if (&UIApplicationWillEnterForegroundNotification) { //Safe to assume multitasking support } else { //Fail gracefully }
enumeration values
请看原文Checking for the existence of enumeration or bit mask values — the kind that you would find inside an NS_ENUM or NS_OPTIONS declaration — is incredibly difficult to check at runtime. Why?
Under the hood, an enumeration is just a method of giving names to numeric constants. An enum is replaced with an int when compiled, which always exists!
If you’re faced with the situation of needing to see if a certain enumeration exists, all you can do is either explicitly check the OS version (which isn’t recommended) or alternatively check against another API element that was introduced at the same time as the new enumeration value.
Note: Whichever way you do this, be sure to add adequate comments to any such code and consider wrapping the code in a dedicated compatibility helper.
显式版本检测
NSString *osVersion = [[UIDevice currentDevice] systemVersion];, 但作者不推荐。。。
最后,水果官方的兼容性指南:SDK Compatibility Guide
相关文章推荐
- IOS开发之不同版本适配问题3(#ifdef __IPHONE_7_0 BaseSDK Development Target)
- iOS不同版本适配问题(#ifdef __IPHONE_7_0)
- 关于MacOS High Sierra版本对旧设备适配问题。 无法启动 损坏硬盘格式
- iOS不同版本适配问题(#ifdef __IPHONE_7_0)
- IOS移动app开发中多种设备尺寸适配问题
- iOS不同版本适配问题(#ifdef __IPHONE_7_0)
- iOS开发日志 之 iOS10版本适配问题
- iOS 5.1.1 设备无法安装AdHoc版本的问题
- IOS开发之不同版本适配问题2(#ifdef __IPHONE_7_0)
- iOS不同版本适配问题
- IOS UIGraphicsBeginImageContextWithOptions 版本适配问题
- IOS开发之不同版本适配问题2(#ifdef __IPHONE_7_0)
- iOS 之版本适配问题
- iOS不同版本适配问题(#ifdef __IPHONE_7_0)
- iOS 10 版本适配问题收集-b
- ios 设备版本的问题
- IOS开发之不同版本适配问题2(#ifdef __IPHONE_7_0)(转载)
- IOS开发之不同版本适配问题
- IOS开发之不同版本适配问题3(#ifdef __IPHONE_7_0 BaseSDK Development Target)
- iOS - 项目更换百度地图2.5版本, 适配iPhone6和iPhone6 plus出现的问题