With
Version class that is contained in
nv-ios-version project (Apache License, Version 2.0), it is easy to get and compare iOS version. An example code below dumps the iOS version and check whether the version is greater than or equal to 6.0.
// Get the system version of iOS at runtime.
NSString *versionString = [[UIDevice currentDevice] systemVersion];
// Convert the version string to a Version instance.
Version *version = [Version versionWithString:versionString];
// Dump the major, minor and micro version numbers.
NSLog(@"version = [%d, %d, %d]",
version.major, version.minor, version.micro);
// Check whether the version is greater than or equal to 6.0.
if ([version isGreaterThanOrEqualToMajor:6 minor:0])
{
// The iOS version is greater than or equal to 6.0.
}
// Another way to check whether iOS version is
// greater than or equal to 6.0
if (6 <= version.major)
{
// The iOS version is greater than or equal to 6.0.
}
In
Version API, the valid range of
major,
minor and
micro is between 0 and 255. If an invalid value is given,
NSInvalidArgumentException is thrown. The default values of
major,
minor and
micro are 0.
number in the API is
"(major << 16) | (minor << 8) | micro".
description method returns "
major.minor" when
micro is 0. Otherwise, "
major.minor.micro". For example, "6.1" is returned for version 6.1.0, and "6.1.1" for version 6.1.1.
Version class provides many comparison methods.
#pragma mark -
#pragma mark Comparison
- (NSComparisonResult)compareToMajor:(int)major;
- (NSComparisonResult)compareToMajor:(int)major minor:(int)minor;
- (NSComparisonResult)compareToMajor:(int)major minor:(int)minor micro:(int)micro;
- (NSComparisonResult)compareToNumber:(int)number;
- (NSComparisonResult)compareTo:(Version *)aVersion;
#pragma mark -
#pragma mark Comparison (equal to)
- (BOOL)isEqualToMajor:(int)major;
- (BOOL)isEqualToMajor:(int)major minor:(int)minor;
- (BOOL)isEqualToMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isEqualToNumber:(int)number;
- (BOOL)isEqualTo:(Version *)aVersion;
#pragma mark -
#pragma mark Comparison (not equal to)
- (BOOL)isNotEqualToMajor:(int)major;
- (BOOL)isNotEqualToMajor:(int)major minor:(int)minor;
- (BOOL)isNotEqualToMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isNotEqualToNumber:(int)number;
- (BOOL)isNotEqualTo:(Version *)aVersion;
#pragma mark -
#pragma mark Comparison (less than)
- (BOOL)isLessThanMajor:(int)major;
- (BOOL)isLessThanMajor:(int)major minor:(int)minor;
- (BOOL)isLessThanMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isLessThanNumber:(int)number;
- (BOOL)isLessThan:(Version *)aVersion;
#pragma mark -
#pragma mark Comparison (less than or equal to)
- (BOOL)isLessThanOrEqualToMajor:(int)major;
- (BOOL)isLessThanOrEqualToMajor:(int)major minor:(int)minor;
- (BOOL)isLessThanOrEqualToMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isLessThanOrEqualToNumber:(int)number;
- (BOOL)isLessThanOrEqualTo:(Version *)aVersion;
#pragma mark -
#pragma mark Comparison (greater than)
- (BOOL)isGreaterThanMajor:(int)major;
- (BOOL)isGreaterThanMajor:(int)major minor:(int)minor;
- (BOOL)isGreaterThanMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isGreaterThanNumber:(int)number;
- (BOOL)isGreaterThan:(Version *)aVersion;
#pragma mark -
#pragma mark Comparison (greater than or equal to)
- (BOOL)isGreaterThanOrEqualToMajor:(int)major;
- (BOOL)isGreaterThanOrEqualToMajor:(int)major minor:(int)minor;
- (BOOL)isGreaterThanOrEqualToMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isGreaterThanOrEqualToNumber:(int)number;
- (BOOL)isGreaterThanOrEqualTo:(Version *)aVersion;
Convenience constructors are listed below.
#pragma mark -
#pragma mark Convenience Constructors
+ (Version *)versionWithMajor:(int)major;
+ (Version *)versionWithMajor:(int)major minor:(int)minor;
+ (Version *)versionWithMajor:(int)major minor:(int)minor micro:(int)micro;
+ (Version *)versionWithString:(NSString *)string;
The following sample code shows how to implement
System class that provides
version method which returns a
Version instance for iOS version.
// System.h
#import <Foundation/Foundation.h>
#import "Version.h"
@interface System : NSObject
/**
* Get iOS version.
*/
+ (Version *)version;
@end
// System.m
#import <UIKit/UIDevice.h>
#import "System.h"
static Version *_version;
@implementation System
/**
* Initialization for this class.
*/
+ (void)initialize
{
// Get the system version of iOS at runtime.
NSString *versionString = [[UIDevice currentDevice] systemVersion];
// Convert the version string to a Version instance.
Version *version = [Version versionWithString:versionString];
// Set as a class variable.
_version = version;
}
/**
* Get iOS version.
*/
+ (Version *)version
{
return _version;
}
@end
With
System class implemented like above, code to check iOS version becomes shorter.
if (System.version.major >= 6)
{
// iOS version is greater than or equal to 6.0.
}
Project Page
https://github.com/TakahikoKawasaki/nv-ios-version