iOS开发之版本兼容一,使用#ifdef语法

来源:互联网 发布:阿里云手机系统root 编辑:程序博客网 时间:2024/05/16 06:41

下面举个简单的例子来说明在iOS7.0和iOS6.1(以及更低版本)之间的适配问题(用的是xcode5.0,里边有6.1和7.0两个版本的sdk)

新建一个工程,默认的development target,base sdk以及模拟器的版本都是7.0,在AppDelegate中的didFinishLaunchingWithOptions方法里写下

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. self.window.tintColor = [UIColor redColor];    

然后运行,这样是没有任何错误的。接下来将development target,base sdk以及模拟器的版本都改成6.1(注意默认的xcode是没有6.1的sdk的,需要自己另外导入)。然后运行,就会报错:

也就是说tintColor属性在iOS6.1中根本就没有,在编译时候就会出错。这时候如下加上判断语句也是没有用的,照样报错(预处理,编译,运行的问题这里不再废话)

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {    
  2.     self.window.tintColor = [UIColor redColor];    
  3. }   
遇见这种情况只能加上预处理语句,这样写:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <del>#ifdef __IPHONE_7_0    
  2.     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {    
  3.         self.window.tintColor = [UIColor redColor];    
  4.     }    
  5. #endif</del>  


[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000  
  2. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {    
  3.         self.window.tintColor = [UIColor redColor];    
  4.     }    
  5. #endif  

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. 其中__IPHONE_OS_VERSION_MAX_ALLOWED定义在usr/include/AvailabilityInternal.h中  
  2. #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED  
  3. /* make sure a default max version is set */  
  4. #ifndef __IPHONE_OS_VERSION_MAX_ALLOWED  
  5.         #define __IPHONE_OS_VERSION_MAX_ALLOWED     __IPHONE_7_0  
  6.     #endif  
  7. /* make sure a valid min is set */  
  8.     #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_2_0  
  9.         #undef __IPHONE_OS_VERSION_MIN_REQUIRED  
  10.         #define __IPHONE_OS_VERSION_MIN_REQUIRED    __IPHONE_2_0   
  11.     #endif  
  12. 。。。。  
  13. #endif  
  14. 注意需要加入头文件#import </usr/include/Availability.h>  
  15. 这样编译通过就不会报错了……这是因为在sdk6.1下的usr/include下边有一个Availability.h文件,里边定义了一大堆宏,其中关于iphone的有  
  16. #define __IPHONE_2_0     20000    
  17. #define __IPHONE_2_1     20100    
  18. #define __IPHONE_2_2     20200    
  19. #define __IPHONE_3_0     30000    
  20. #define __IPHONE_3_1     30100    
  21. #define __IPHONE_3_2     30200    
  22. #define __IPHONE_4_0     40000    
  23. #define __IPHONE_4_1     40100    
  24. #define __IPHONE_4_2     40200    
  25. #define __IPHONE_4_3     40300    
  26. #define __IPHONE_5_0     50000    
  27. #define __IPHONE_5_1     50100    
  28. #define __IPHONE_6_0     60000    
  29. #define __IPHONE_6_1     60100    
  30. #define __IPHONE_NA      99999  /* not available */   

而sdk7.0里边多了一行 

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #define __IPHONE_7_0 70000  
iOS8当然也是一样的道理

除此之外,当使用framework或者.a的时候需要注意

因为在编译framework或者.a的时候是依赖当时编译的环境决定的,如使用iOS6的sdk可以将framework或者.a顺利编译通过,但是在真正生成app的时候需要保证编译app时的sdk和编译framework或者.a的sdk是一致的,且同时需要加入运行时判断([[[UIDevice currentDevice] systemVersion] floatValue]那个)

说明:

1、如果是使用低版本sdk编译的(如iOS6),将最终的应用安装至高版本的设备上(iOS7的系统),此时应该不会出现问题,因为iOS7兼容iOS6,但是无法使用iOS7特有的新功能,因为代码被屏蔽了。

2、如果是使用高版本sdk编译的(如iOS7),将最终的应用安装至低版本的设备上(iOS6的系统),此时如果不加入运行时判断就会出现问题(可能是crash)。因为应用本身是用iOS7编译的,并使用iOS7才有的新功能,而设备并不支持iOS7。


0 0
原创粉丝点击