4
\$\begingroup\$

Here's an approach i came up with for checking IOS versions.

#define __PROCESS_INFO ([NSProcessInfo processInfo])
#define CURRENT_IOS ([__PROCESS_INFO operatingSystemVersion])
#define IOS(x) ((NSOperatingSystemVersion){[x integerValue], 0, 0})
#define IOS9 ((NSOperatingSystemVersion){9, 0, 0})
#define IOS10 ((NSOperatingSystemVersion){10, 0, 0})
#define IOS11 ((NSOperatingSystemVersion){11, 0, 0})
#define IS_IOS9 ([__PROCESS_INFO isOperatingSystemAtLeastVersion:IOS9] && \
 ![__PROCESS_INFO isOperatingSystemAtLeastVersion:IOS10])
#define IS_IOS10 ([__PROCESS_INFO isOperatingSystemAtLeastVersion:IOS10] && \
 ![__PROCESS_INFO isOperatingSystemAtLeastVersion:IOS11])

that way this type of code is more readable

if (IS_IOS9) {
 // [... openURL: ...];
}
else if (IS_IOS10) {
 // [... openURL:options:completionHandler: ...];
}

Let me know what you think ...

asked Oct 14, 2016 at 21:29
\$\endgroup\$
1

1 Answer 1

8
\$\begingroup\$

If you need to check if a certain method exists you can also check if the object supports it via respondsToSelector: like this:

if ([object respondsToSelector:@selector(openURL:options:completionHandler:)]) {
 [object openURL:...options:...completionHandler:...];
} else {
 [object openURL:...];
}

I think this approach is more flexible than checking the OS version. I've seen many people doing this.

This is similar to how JavaScript programmers check existence of certain JS features: if they checked the browser version instead, the code would be unmaintainable as there are a lot of browsers and they constantly evolve.

answered Nov 5, 2016 at 16:08
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.