Skip to content

Instantly share code, notes, and snippets.

@tettoffensive
Forked from krin-san/iOSVersionCheck.h
Created September 8, 2016 19:30
Show Gist options
  • Save tettoffensive/59400a71d550eb5d66076db44a2c0da4 to your computer and use it in GitHub Desktop.
Save tettoffensive/59400a71d550eb5d66076db44a2c0da4 to your computer and use it in GitHub Desktop.
NSProcessInfo-based iOS version check macro (iOS 8+)
#define INC_SYSTEM_VERSION(v) ((NSOperatingSystemVersion){v.majorVersion, v.minorVersion + 1, 0})
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) [[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:v]
#define SYSTEM_VERSION_LESS_THAN(v) (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v))
#define SYSTEM_VERSION_EQUAL_TO(v) (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) && (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(INC_SYSTEM_VERSION(v))))
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) (SYSTEM_VERSION_LESS_THAN(v) || SYSTEM_VERSION_EQUAL_TO(v))
#define SYSTEM_VERSION_GREATER_THAN(v) (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) && (!SYSTEM_VERSION_EQUAL_TO(v)))
/*
// Manual logical check
NSOperatingSystemVersion v = (NSOperatingSystemVersion){8, 4, 0};
NSOperatingSystemVersion (^inc)(NSOperatingSystemVersion) = ^(NSOperatingSystemVersion v){
return (NSOperatingSystemVersion){v.majorVersion, v.minorVersion + 1, 0};
};
BOOL greaterThanOrEqual = [[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:v];
BOOL lessThan = !greaterThanOrEqual;
BOOL equal = greaterThanOrEqual && ![[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:inc(v)];
BOOL lessThanOrEqual = lessThan || equal;
BOOL greaterThan = greaterThanOrEqual && !equal;
DLog(@"<= < == > >= | %@", [NSProcessInfo processInfo].operatingSystemVersionString);
DLog(@"%i %i %i %i %i", lessThanOrEqual, lessThan, equal, greaterThan, greaterThanOrEqual);
DLog(@"%i %i %i %i %i", SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v), SYSTEM_VERSION_LESS_THAN(v), SYSTEM_VERSION_EQUAL_TO(v), SYSTEM_VERSION_GREATER_THAN(v), SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v));
*/
extension NSProcessInfo {
@available(iOS 8.0, *)
public func systemVersionGreaterThanOrEqualTo(version: NSOperatingSystemVersion) -> Bool {
return isOperatingSystemAtLeastVersion(version)
}
@available(iOS 8.0, *)
public func systemVersionGreaterThan(version: NSOperatingSystemVersion) -> Bool {
return systemVersionGreaterThanOrEqualTo(version) && !systemVersionEqualTo(version)
}
@available(iOS 8.0, *)
public func systemVersionEqualTo(version: NSOperatingSystemVersion) -> Bool {
return (systemVersionGreaterThanOrEqualTo(version) && !systemVersionGreaterThanOrEqualTo(incrementSystemVersion(version)))
}
@available(iOS 8.0, *)
public func systemVersionLessThan(version: NSOperatingSystemVersion) -> Bool {
return !systemVersionGreaterThanOrEqualTo(version)
}
@available(iOS 8.0, *)
public func systemVersionLessThanOrEqualTo(version: NSOperatingSystemVersion) -> Bool {
return systemVersionLessThan(version) || systemVersionEqualTo(version)
}
/// Increment minor OS version and drop 'patch' version (e.g.: 8.1.1 -> 8.2.0)
private func incrementSystemVersion(var version: NSOperatingSystemVersion) -> NSOperatingSystemVersion {
version.minorVersion++
version.patchVersion = 0
return version
}
}
/*
// Manual logical check
let v = NSOperatingSystemVersion(majorVersion: 8, minorVersion: 4, patchVersion: 0)
let greaterThanOrEqual = NSProcessInfo.processInfo().isOperatingSystemAtLeastVersion(v)
let lessThan = !greaterThanOrEqual
let equal = greaterThanOrEqual && !NSProcessInfo.processInfo().isOperatingSystemAtLeastVersion(incrementSystemVersion(v))
let lessThanOrEqual = lessThan || equal
let greaterThan = greaterThanOrEqual && !equal
dlog("<= < == > >= | \(NSProcessInfo.processInfo().operatingSystemVersionString)")
dlog("\(lessThanOrEqual ? 1 : 0) \(lessThan ? 1 : 0) \(equal ? 1 : 0) \(greaterThan ? 1 : 0) \(greaterThanOrEqual ? 1 : 0)")
dlog("\(NSProcessInfo.processInfo().systemVersionLessThanOrEqualTo(v) ? 1 : 0) \(NSProcessInfo.processInfo().systemVersionLessThan(v) ? 1 : 0) \(NSProcessInfo.processInfo().systemVersionEqualTo(v) ? 1 : 0) \(NSProcessInfo.processInfo().systemVersionGreaterThan(v) ? 1 : 0) \(NSProcessInfo.processInfo().systemVersionGreaterThanOrEqualTo(v) ? 1 : 0)")
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment