Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define code for different iOS versions

I am working on an iOS app. I want it to support iOS 7 and 8. It is going pretty nicely, however there are lots of different parts of the app which use Apple APIs. Some of these APIs work in both iOS 8 and 7. However, some of them are deprecated in iOS 8. So I therefore went to the Apple developer site to see what to replace them with (new methods/etc....).

However, I now have the problem that the app will work on iOS 8 fine, but certain parts of it don't work properly on iOS 7 as I'm trying to use an iOS 8 API...... (lol).

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

IDEA 1

Whenever I have code which doesn't work on both OS's, I use an if function (which calls a macro) like so:

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 all around the app like so:

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

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

Or are both my ideas rubbish? Is there a much better way about solving this problem?

Thanks for your time, Dan.

like image 446
Supertecnoboff Avatar asked Jan 10 '23 03:01

Supertecnoboff


1 Answers

I don't suggest checking the Version and writing code based on that. Instead you need to check whether that API is available or not.

For checking a class available or not:

Class checkClass = NSClassFromString(@"CheckingClass");

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

If you need to check a feature/function available;

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

NOTE:

Deprecated API's doesn't mean that you shouldn't use that in current version. It means, it won't work from next version onwards, and only work till current version.

like image 154
Midhun MP Avatar answered Jan 14 '23 17:01

Midhun MP