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 ...

\$\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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.