现在的位置: 首页 > 综合 > 正文

SDK兼容性引导

2013年10月21日 ⁄ 综合 ⁄ 共 4778字 ⁄ 字号 评论关闭

SDK兼容性引导

基于SDK基础的开发

介绍说明如何应用于XCode工程的基于SDK开发的技术
1、用(weakly linked)弱连接类、方法和函数来支持在不同版本之间的程序运行
2、弱连接整个框架(framework)
3、为不同的SDK选择不同的编译条件
4、在代码中找出过时API的使用
5、确定在运行时操作系统和框架(framework)的版本


一 、在IOS中使用弱连接类


在工程中使用类的弱连接的时候必须确保这些类在运行时的可用性,要不会引起动态连接的错误
在IOS4.2以后的版本都是使用NSObject class的方法来检测弱连接在运行时态的可用性,这种简单高效的机制使用了NS_CLASS_AVAILABLE的
可用性宏。
********

作者 :禚来强  原文地址:http://blog.csdn.net/diyagoanyhacker/article/details/6673344 email:zhuolaiqiang@gmail.com
 电话:18671682672     转文请保留


检测最近release的framework还不支持NS_CLASS_AVAILABLE的宏

在支持NS_CLASS_AVAILABLE的宏framework的条件编译中,可以如下的使用

if ([UIPrintInteractionController class]) { 
    // Create an instance of the class and use it. 
} else { 
    // Alternate code path to follow when the 
    // class is not available. 
}

如果你在不确保是否已经可以使用类方法的时候你可以使用NSClassFromString 方法来判断
使用方法如下:

Class cls = NSClassFromString (@"NSRegularExpression"); 
if (cls) { 
    // Create an instance of the class and use it. 
} else { 
    // Alternate code path to follow when the 
    // class is not available. 
}


二、在方法,函数和符号中使用弱连接


和使用类的弱连接一样,在使用它之前要确保方法函数和符号在运行时的可用性,要不在编译的时候会报错动态连接错误

假设你想使用新版本SDK的特性但是又想能够运行在低版本的SDK中,那么就要对早期的版本设置相应的开发target

在Object-c中 instancesRespondToSelector: 方法告诉我们所给的方法是否可用
例如:使用 availableCaptureModesForCameraDevice:这个方法(在4.0以后才是可用的),我们可以这样使用它

1、检查一个Object-c方法的可用性
if ([UIImagePickerController instancesRespondToSelector: 
              @selector (availableCaptureModesForCameraDevice:)]) { 
    // Method is available for use. 
    // Your code can check if video capture is available and, 
    // if it is, offer that option. 
} else { 
    // Method is not available. 
    // Alternate code to use only still image capture. 
}

判断一个弱连接的c函数是否可用,只要判断函数的地址是否返回为NULL,以CGColorCreateGenericCMYK 函数为例,我们可以像以下那样使用。

2、检查c方法的可用性
if (CGColorCreateGenericCMYK != NULL) { 
    CGColorCreateGenericCMYK (0.1,0.5.0.0,1.0,0.1); 
} else { 
    // Function is not available. 
    // Alternate code to create a color object with earlier technology 

*********
要检测一个C方法是否可用,比较明确的为地址是否为NULL或零。你不能使用反运算符(!)来否定一个函数的可用性

检测一个 external(extern)常量或一个通知的名字应当比较它的地址(address)--而不是符号的名称, 判断是否为NULL or nil

三、弱连接整个Framework


比如一个在高版本中才出现的Framework,想在低版本使用他的特性。那你就必须弱连接那个使用的Framework
详见官方的图解---(其实就是在添加进去的Framework的 required 改成 optional)

http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/XcodeProjectManagement/130-Files_in_Projects/project_files.html#//apple_ref/doc/uid/TP40002666-SW4


四、条件编译for不同的SDK


如果你不止基于一个SDK编译,你就可能需要为base sdk使用条件化,可以使用在Availability.h中的定义。
*****
这个.h文件存在于系统的文件夹/usr/include的文件夹下 
例如想在Mac OS X v10.5(而不是IOS)中使用函数 CGColorCreateGenericCMYK

使用预处理指令for条件编译
#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED 
    // code only compiled when targeting Mac OS X and not iOS 
    // note use of 1050 instead of __MAC_10_5 
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 
    if (CGColorCreateGenericCMYK != NULL) { 
        CGColorCreateGenericCMYK(0.1,0.5.0.0,1.0,0.1); 
    } else { 
#endif 
    // code to create a color object with earlier technology 
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 
    } 
#endif 
#endif 
}


五、寻找出在程序中使用的以过时的实例


在IOS或Mac OS中有时候API会过时,但是过时不代表着那些就从Library或framework中删除,但是在使用的过程中会报出warning,
并且在不远的将来可能会被Apple从中移除。
例如我们在code中使用了过时的函数 HPurge那么就会报出如下
'HPurge' is deprecated (declared at /Users/steve/MyProject/main.c:51)
所以我们应当在工程中查找出如下的警告并且修改。

六、确定操作系统和Framework的版本


* 在运行时检查IOS的版本
  NSString *osVersion = [[UIDevice currentDevice] systemVersion];

* 在运行时检查Mac OS X用Gestalt function 和 系统版本常量

另外,对于许多的Framework你可以在运行时检查指定Framework的版本。
例如:Application Kit(NSApplication.h)定义了NSAppKitVersionNumber常量---可以用来检查Application Kit Framework的版本

APPKIT_EXTERN double NSAppKitVersionNumber; 
#define NSAppKitVersionNumber10_0 577 
#define NSAppKitVersionNumber10_1 620 
#define NSAppKitVersionNumber10_2 663 
#define NSAppKitVersionNumber10_2_3 663.6 
#define NSAppKitVersionNumber10_3 743 
#define NSAppKitVersionNumber10_3_2 743.14 
#define NSAppKitVersionNumber10_3_3 743.2 
#define NSAppKitVersionNumber10_3_5 743.24 
#define NSAppKitVersionNumber10_3_7 743.33 
#define NSAppKitVersionNumber10_3_9 743.36 
#define NSAppKitVersionNumber10_4 824 
#define NSAppKitVersionNumber10_4_1 824.1 
#define NSAppKitVersionNumber10_4_3 824.23 
#define NSAppKitVersionNumber10_4_4 824.33 
#define NSAppKitVersionNumber10_4_7 824.41 
#define NSAppKitVersionNumber10_5 949 
#define NSAppKitVersionNumber10_5_2 949.27 
#define NSAppKitVersionNumber10_5_3 949.33

所以我们可以像如下使用:
if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_0) { 
  /* On a 10.0.x or earlier system */ 
} else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_1) { 
  /* On a 10.1 - 10.1.x system */ 
} else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_2) { 
  /* On a 10.2 - 10.2.x system */ 
} else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_3) { 
  /* On 10.3 - 10.3.x system */ 
} else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_4) { 
  /* On a 10.4 - 10.4.x system */ 
} else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_5) { 
  /* On a 10.5 - 10.5.x system */ 
} else { 
  /* 10.6 or later system */ 
}


跟以上一样在 NSObjCRuntime.h中用定义了NSFoundationVersionNumber全局常量


官网的连接地址

http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/cross_development/Using/using.html

抱歉!评论已关闭.