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
- if ([UIPrintInteractionController class]) {
- // Create an instance of the class and use it.
- } else {
- // Alternate code path to follow when the
- // 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:
- 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.
- }
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
- 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.
- }
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.
- 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
- }
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)
- http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/XcodeProjectManagement/
- 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
- #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
- }
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:
- '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
- 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
- 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
So we can use it as follows:
- 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 */
- }
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