How to determine the code for different versions of iOS

I am working on an iOS app. I want it to support iOS 7 and 8. This happens pretty nicely, however there are many different parts of the application that use the Apple API. Some of these APIs work both in iOS 8 and 7. However, some of them are deprecated in iOS 8. So I went to the Apple developer site to see what to replace them (new methods / etc ....).

However, I now have a problem that the application will work on iOS 8 perfectly, but some of its parts do not work properly on iOS 7, since I'm trying to use the iOS 8 API ... (laughs).

So, I just wanted to know what the best way to implement code that works on iOS 8 and 7. I had a few ideas (below), but I'm not sure which is better:

IDEA 1

Whenever I have code that does not work on both OSs, I use the if function (which calls the macro) as follows:

if (SYSTEM_VERSION_LESS_THAN(@"8.0")) {
    // iOS 7 device. Use iOS 7 apis.
}

else {
   // iOS 8 (or higher) - use iOS 8 apis.
}

IDEA 2

I was thinking about using ifdef definitions around the application, for example:

#ifdef __IPHONE_8_0
     // iOS 8 code here....
#else
     // iOS 7 code here....
#endif

Which way is better? I would think that the second idea is much faster and uses less resources?

Or both of my garbage? Is there a better way to solve this problem?

Thanks for your time, Dan.

+4
source share
2 answers

I do not suggest checking the version and writing code based on this. Instead, you need to check if this API is available or not.

To check the availability of a class or not:

Class checkClass = NSClassFromString(@"CheckingClass");

if (checkClass)
{
   // Available
}
else
{
  // Not Available
}

If you need to check an available function / function,

if ([checkClass respondsToSelector:@selector(yourMethod:)])
{
   // Feature/ Method Available
}
else
{
   // Feature/ Method Not Available
}

Note:

API , . , .

+6

ifdef -way , ; , ios .

, , Mac OS X iOS , , Mac OS iOS.

, 1 - , , respondsToSelector: , iOS, .

, , , API, ios7.

+2

Source: https://habr.com/ru/post/1570717/


All Articles