轉自:http://blog.csdn.net/linkai5696/archive/2010/12/12/6071558.aspx
基於SDK基礎的開發
介紹說明如何應用於XCode工程的基於SDK開發的技術
1、用(weakly linked)弱串連類、方法和函數來支援在不同版本之間的程式運行
2、弱串連整個架構(framework)
3、為不同的SDK選擇不同的編譯條件
4、在代碼中找出過時API的使用
5、確定在運行時作業系統和架構(framework)的版本
一 、在IOS中使用弱串連類
在工程中使用類的弱串連的時候必須確保這些類在運行時的可用性,要不會引起動態串連的錯誤
在IOS4.2以後的版本都是使用NSObject class的方法來檢測弱串連在運行時態的可用性,這種簡單高效的機制使用了NS_CLASS_AVAILABLE的
可用性宏。
********
檢測最近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