Ios sdk Compatibility Guide

Source: Internet
Author: User

IOS SDKCompatibility guidance is the content to be introduced in this article, mainly based onIOS SDKThe basic development description explains how to apply it to the XCode project based onIOS SDKDeveloped technology. Let's take a look at the details.

1. Use (weakly linked) Weak Connection classes, methods, and functions to support program running between different versions

2. Weak Connection to the entire framework)

3. Select different compiling conditions for different IOS sdks

4. Identify out-of-date API usage in the code

5. Determine the operating system and framework version at runtime.

I. Use Weak Connection classes in IOS

When using weak connections of classes in the project, you must ensure the availability of these classes at runtime without causing dynamic connection errors.

In Versions later than IOS4.2, NSObject class is used to detect the availability of weak connections in the runtime tense. This simple and efficient mechanism uses the availability macro of NS_CLASS_AVAILABLE.

Detects that the latest release framework does not support macros of NS_CLASS_AVAILABLE.

In Conditional compilation of macro framework that supports NS_CLASS_AVAILABLE, you can use the following

 
 
  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.   

If you are not sure whether you can use the class method, you can use the NSClassFromString method to determine whether to use the class method. The method is as follows:

 
 
  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.   

2. Use weak connections in methods, functions, and symbols

Like using a weak connection of a class, before using it, you must ensure the availability of method functions and symbols at runtime, or report a dynamic connection error during compilation, if you want to use the features of the new ios sdk but want to run it in a lower version of the SDK, you need to set the corresponding development target for the earlier version, and instancesRespondToSelector in Object-c: the method tells us whether the given method is available. For example, use availableCaptureModesForCameraDevice: This method (available after 4.0). We can use it like this.

1. Check the availability of an Object-c method

 
 
  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.   

To determine whether a weak-connection C function is available, as long as the address of the function is returned as NULL, The CGColorCreateGenericCMYK function can be used as follows.

2. Check the availability of method 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. }  

To check whether a C method is available, it is clear that the address is NULL or zero. You cannot use the inverse operator (!) To deny the availability of a function.

Checks whether an external (extern) constant or notification name should compare its address (address) -- instead of the symbol name to determine whether it is NULL or nil

Iii. Weak Connection to the entire Framework

For example, a Framework that only appears in a later version wants to use its features in a later version. Then you must connect to the Framework that you use. For details, see the official illustration --- (in fact, change required of the added Framework to 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 

4. Conditional compilation for different sdks

If you compile based on more than one ios sdk, you may need to use conditional for the base sdk, which can be defined in Availability. h. This. h file is stored in the system folder/usr/include. For example, you want to use the CGColorCreateGenericCMYK function in Mac OS X v10.5 (instead of IOS ).

Use pre-processing commands for Conditional compilation

 
 
  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   

5. Search for outdated instances used in the program

In IOS or Mac OS, sometimes the API is outdated, but it does not mean that those APIs are deleted from the Library or framework. However, during use, the system reports a warning, and may be removed from Apple in the near future. For example, if we use the obsolete function HPurge in the code, the following error is reported:

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

So we should find the following warning in the project and modify it.

6. Determine the operating system and Framework Version

Check the IOS version at runtime

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

Check Gestalt function and System Version constant for Mac OS X at runtime

In addition, for many frameworks, you can check the version of the specified Framework at runtime.

For example, Application Kit (NSApplication. h) defines the NSAppKitVersionNumber constant, which can be used to check the version of the Application Kit Framework.

For example

 
 
  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 

So we can use it as follows:

 
 
  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 */   

As mentioned above, the NSFoundationVersionNumber global constant is defined in NSObjCRuntime. h.

Summary: DetailsIOS SDKWe hope that this article will be helpful to you!

Address: http://blog.csdn.net/diyagoanyhacker/article/details/6673344

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.