Ways to meet iOS system compatibility requirements

Source: Internet
Author: User
Tags deprecated old windows

Reproduced herein please retain the following original author information: Original: Onev's Den http://www.onevcat.com/2012/02/iosversion/

Compatibility, Developer's shame

The compatibility issue is one of the most vexing problems in developing the world, and no developer will refute it. The horror of the old Windows Vista upgrade of a bunch of apps is vivid, and now there are similar problems on the hot mobile internet platform. Andriod Open source leads to a large number of producers, different devices not only hardware configuration differences, system differences, even the screen scale is also different. It's impossible to develop an app that really works perfectly on all Android platforms. iOS is a lot better because the iphone and itouch have exactly the same resolution (even if the retina has apple to take care of it), so there's often only a system version difference. it is necessary to determine the operating environment when using the features of some new systems, or the methods that are discarded.

When to take care of compatibility

No one can remember all the new classes or attribute methods that are added to the system, and no one can develop an application that is amazing for the whole system.

in iOS, the first thing to do is to choose the right minimum version for your app , and in the target property of the corresponding app in Xcode, set the iOS Deployment target property, This corresponds to the lowest system version your app will run on. If you're setting up iOS 4.3, you don't have to think about the compatibility of your code with a system before 4.3-because devices below iOS 4.3 can't install your app through the regular channels.

But you still have to consider the compatibility of the system after this. As Apple continues to upgrade its system, some features or features can be easily and efficiently implemented on the new system version, and some old APIs are deprecated or deleted. If these potential compatibility issues are not addressed, the small program is inefficient, difficult to maintain, and crash directly. For these possible problems, developers must be sure that the API they call is in the framework they are using. The system versions supported by a class or method can be found in the official documentation, and all entries will have similar instructions.

Availability Available in IOS 5.0 and later.

Or

Deprecated in IOS 5.0. Version-compatible information can be learned from availability. If the iOS Deployment target is smaller than the system requirements, you will need to write the code separately, otherwise the feature you are using cannot be performed on the older version of the system.

In addition, Apple is just a company, Apple developers are just a little bit of a programmer, iOS itself may have some bugs, resulting in some versions of defects. Developers will be very care, and fixed as soon as possible, but many times the user is not care, or for various reasons have not upgraded the system. At this point, the system needs to be detected to avoid these bugs in order to increase the user experience (such as when the UITableView insert row at 3.0 may be crash).

Code determination system version that meets compatibility requirements, run code by version number at run time

This is not necessarily the most efficient approach, but it is the most straightforward approach. The decision on the system version can refer to another piece of my article: Uiviewcontroller misuse. By the way, the comparison is also posted here:

System Versioning Preprocessor Macros#Define SYSTEM_VERSION_EQUAL_TO (v) ([[[[Uidevice Currentdevice] systemversion] compare:v options:nsnumericsearch] = = Nsorderedsame)#define System_version_greater_than (v) ([[[[Uidevice Currentdevice] systemversion] compare:v options: Nsnumericsearch] = = nsordereddescending)#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO (v) ([[[[Uidevice] Currentdevice] systemversion] compare:v options:nsnumericsearch]! = nsorderedascending)#define SYSTEM_ Version_less_than (v) ([[[Uidevice Currentdevice] systemversion] compare:v options:nsnumericsearch] = = nsorderedascending)#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO (v) ([[[Uidevice Currentdevice] Systemversion] Compare:v options:nsnumericsearch]! = nsordereddescending)       

Use ... if (Systemversion Less_than (@ "5.0")) {//Systems version is not less than 5.0 ... Do something}

It is important to note that [@“5.0” compare:@“5” options:NSNumericSearch] such input will return a nsordereddescending instead of the expected nsorderedsame, so you need to be very careful when you enter it. In addition, the limitation of this method is that for 4.1.3 such a system version number is weak, want to use the words may need to be converted (perhaps to remove the second decimal point and then compare is a feasible solution).

For a class, you can detect whether it exists

If the new attribute to be used is a class, not a specific method, you can use Nsclassfromstring to convert the string to a class, and if the runtime does not exist for the class, the method returns nil. For example, for iOS 5, in terms of location resolution, Apple uses the new class Clgeocoder in Corelocation instead of the original mkreversegeocoder to determine which class to use, using the following code:

if(NSClassFromString(@"CLGeocoder")) {      //CLGeocoder存在,可以使用}else{      //只能使用MKReverseGeocoder}

This method should be absolutely reliable, but only classes can use it. and system updates often bring more than classes, there are many new methods and properties of such things, this way is powerless.

Ask a specific class or object if you have an attribute

For some of the current-account members, they have similar queries that do not support a particular feature, such as the Availablemediatypesforsourcetype: method for detecting the available media types in the Uiimagepickercontroller, which is well known. And the method of detecting whether the three-axis gyroscope is available in Cmmotionmanager gyroavailable. However, some classes and members that ask for methods are basically about device hardware.

Alternatively, you can use the Respondstoselector: method to detect whether a method responds. But this is very dangerous practice , I do not recommend the use of. Because there are a lot of private APIs in the iOS system, Apple's review mechanism is known to be the industry's only shoot for private API calls. and Respondstoselector: If the method you want to detect is in the old version of the system as a private API, you will get the wrong result to execute the private API, then it's over. This application is often rejected without knowing where to use the private API ...

Ways to meet iOS system compatibility requirements

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.