HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Check for iOS version

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
versioncheckforios

Problem

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

Solution

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.

Code Snippets

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

Context

StackExchange Code Review Q#144255, answer score: 8

Revisions (0)

No revisions yet.