詳解IOS SDK相容性引導

來源:互聯網
上載者:User

IOS SDK相容性引導是本文要介紹的內容,主要是基於IOS SDK基礎的開發介紹說明如何應用於XCode工程的基於 IOS SDK開發的技術。來看詳細內容講解。

1、用(weakly linked)弱串連類、方法和函數來支援在不同版本之間的程式運行

2、弱串連整個架構(framework)

3、為不同的IOS SDK選擇不同的編譯條件

4、在代碼中找出過時API的使用

5、確定在運行時作業系統和架構(framework)的版本

一 、在IOS中使用弱串連類

在工程中使用類的弱串連的時候必須確保這些類在運行時的可用性,要不會引起動態串連的錯誤。

在IOS4.2以後的版本都是使用NSObject class的方法來檢測弱串連在運行時態的可用性,這種簡單高效的機制使用了NS_CLASS_AVAILABLE的可用性宏。

檢測最近release的framework還不支援NS_CLASS_AVAILABLE的宏

在支援NS_CLASS_AVAILABLE的宏framework的條件編譯中,可以如下的使用

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

如果你在不確保是否已經可以使用類方法的時候你可以使用NSClassFromString 方法來判斷,使用方法如下:

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

二、在方法,函數和符號中使用弱串連

和使用類的弱串連一樣,在使用它之前要確保方法函數和符號在運行時的可用性,要不在編譯的時候會報錯動態串連錯誤,假設你想使用新版本IOS SDK的特性但是又想能夠運行在低版本的SDK中,那麼就要對早期的版本設定相應的開發target,在Object-c中 instancesRespondToSelector: 方法告訴我們所給的方法是否可用,例如:使用availableCaptureModesForCameraDevice:這個方法(在4.0以後才是可用的),我們可以這樣使用它。

1、檢查一個Object-c方法的可用性

 
  1. if ([UIImagePickerController instancesRespondToSelector:   
  2.               @selector (availableCaptureModesForCameraDevice:)]) {   
  3.     // Method is available for use.   
  4.     // Your code can check if video capture is available and,   
  5.     // if it is, offer that option.   
  6. } else {   
  7.     // Method is not available.   
  8.     // Alternate code to use only still image capture.   

判斷一個弱串連的C函數是否可用,只要判斷函數的地址是否返回為NULL,以CGColorCreateGenericCMYK 函數為例,我們可以像以下那樣使用。

2、檢查C方法的可用性

 
  1. if (CGColorCreateGenericCMYK != NULL) {   
  2.     CGColorCreateGenericCMYK (0.1,0.5.0.0,1.0,0.1);   
  3. } else {   
  4.     // Function is not available.   
  5.     // Alternate code to create a color object with earlier technology   
  6. }  

要檢測一個C方法是否可用,比較明確的為地址是否為NULL或零。你不能使用反運算子(!)來否定一個函數的可用性

檢測一個 external(extern)常量或一個通知的名字應當比較它的地址(address)--而不是符號的名稱, 判斷是否為NULL or nil

三、弱串連整個Framework

比如一個在高版本中才出現的Framework,想在低版本使用他的特性。那你就必須弱串連那個使用的Framework,詳見官方的圖解---(其實就是在添加進去的Framework的 required 改成 optional)

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

四、條件編譯for不同的SDK

如果你不止基於一個IOS SDK編譯,你就可能需要為base sdk使用條件化,可以使用在Availability.h中的定義。這個.h檔案存在於系統的檔案夾/usr/include的檔案夾下,例如想在Mac OS X v10.5(而不是IOS)中使用函數 CGColorCreateGenericCMYK

使用預先處理指令for條件編譯

 
  1. #ifdef __MAC_OS_X_VERSION_MAX_ALLOWED   
  2.     // code only compiled when targeting Mac OS X and not iOS   
  3.     // note use of 1050 instead of __MAC_10_5   
  4. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050   
  5.     if (CGColorCreateGenericCMYK != NULL) {   
  6.         CGColorCreateGenericCMYK(0.1,0.5.0.0,1.0,0.1);   
  7.     } else {   
  8. #endif   
  9.     // code to create a color object with earlier technology   
  10. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050   
  11.     }   
  12. #endif   
  13. #endif   

五、尋找出在程式中使用的以過時的執行個體

在IOS或Mac OS中有時候API會過時,但是過時不代表著那些就從Library或framework中刪除,但是在使用的過程中會報出warning,並且在不遠的將來可能會被Apple從中移除。例如我們在code中使用了過時的函數 HPurge那麼就會報出如下

 
  1. 'HPurge' is deprecated (declared at /Users/steve/MyProject/main.c:51) 

所以我們應當在工程中尋找出如下的警告並且修改。

六、確定作業系統和Framework的版本

在運行時檢查IOS的版本

 
  1. NSString *osVersion = [[UIDevice currentDevice] systemVersion]; 

在運行時檢查Mac OS X用Gestalt function 和 系統版本常量

另外,對於許多的Framework你可以在運行時檢查指定Framework的版本。

例如:Application Kit(NSApplication.h)定義了NSAppKitVersionNumber常量---可以用來檢查Application Kit Framework的版本

 
  1. APPKIT_EXTERN double NSAppKitVersionNumber;   
  2. #define NSAppKitVersionNumber10_0 577   
  3. #define NSAppKitVersionNumber10_1 620   
  4. #define NSAppKitVersionNumber10_2 663   
  5. #define NSAppKitVersionNumber10_2_3 663.6   
  6. #define NSAppKitVersionNumber10_3 743   
  7. #define NSAppKitVersionNumber10_3_2 743.14   
  8. #define NSAppKitVersionNumber10_3_3 743.2   
  9. #define NSAppKitVersionNumber10_3_5 743.24   
  10. #define NSAppKitVersionNumber10_3_7 743.33   
  11. #define NSAppKitVersionNumber10_3_9 743.36   
  12. #define NSAppKitVersionNumber10_4 824   
  13. #define NSAppKitVersionNumber10_4_1 824.1   
  14. #define NSAppKitVersionNumber10_4_3 824.23   
  15. #define NSAppKitVersionNumber10_4_4 824.33   
  16. #define NSAppKitVersionNumber10_4_7 824.41   
  17. #define NSAppKitVersionNumber10_5 949   
  18. #define NSAppKitVersionNumber10_5_2 949.27   
  19. #define NSAppKitVersionNumber10_5_3 949.33 

所以我們可以像如下使用:

 
  1. if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_0) {   
  2.   /* On a 10.0.x or earlier system */   
  3. } else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_1) {   
  4.   /* On a 10.1 - 10.1.x system */   
  5. } else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_2) {   
  6.   /* On a 10.2 - 10.2.x system */   
  7. } else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_3) {   
  8.   /* On 10.3 - 10.3.x system */   
  9. } else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_4) {   
  10.   /* On a 10.4 - 10.4.x system */   
  11. } else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_5) {   
  12.   /* On a 10.5 - 10.5.x system */   
  13. } else {   
  14.   /* 10.6 or later system */   

跟以上一樣在 NSObjCRuntime.h中用定義了NSFoundationVersionNumber全域常量

小結:詳解IOS SDK相容性引導的內容介紹玩玩了,希望通過本文的學習能對你有所協助!

原文地址:http://blog.csdn.net/diyagoanyhacker/article/details/6673344

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.