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 ...
-
\$\begingroup\$ Supporting Multiple iOS Versions and Devices \$\endgroup\$NSDeveloper– NSDeveloper2017年04月28日 01:59:12 +00:00Commented Apr 28, 2017 at 1:59
1 Answer 1
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.