Last active
August 29, 2015 14:01
-
-
Save gcox/5cd75a4a484ebf9a770f to your computer and use it in GitHub Desktop.
A class for getting Mac OS system version, bundle versions, and comparison of versions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The MIT License (MIT) | |
Copyright (c) 2014 Tundaware LLC | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
#import <Foundation/Foundation.h> | |
@interface TWVersion : NSObject | |
@property (strong, readonly) NSNumber *major; | |
@property (strong, readonly) NSNumber *minor; | |
@property (strong, readonly) NSNumber *revision; | |
@property (strong, readonly) NSString *build; | |
// First tries to read from the SystemVersion.plist. If this fails for some reason, falls back to using deprecated Gestalt | |
+(TWVersion *)systemVersion; | |
+(TWVersion *)bundleVersion; | |
+(TWVersion *)bundleVersion:(NSBundle *)bundle; | |
// Expects a dot-delimited version number (i.e. 2.5.3) | |
+(TWVersion *)versionWithString:(NSString *)versionString; | |
-(NSString *)marketingVersionString; | |
// Includes the build number where available (i.e. 2.5.3 (359)) | |
-(NSString *)fullVersionString; | |
-(BOOL)isNewerThan:(TWVersion *)version; | |
-(BOOL)isOlderThan:(TWVersion *)version; | |
-(BOOL)isEqualTo:(TWVersion *)object; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The MIT License (MIT) | |
Copyright (c) 2014 Tundaware LLC | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
#import "TWVersion.h" | |
@implementation TWVersion { | |
NSString *_marketingVersionString; | |
NSString *_fullVersionString; | |
} | |
+(TWVersion *)systemVersion { | |
static TWVersion *version; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
version = [TWVersion systemVersionFromPList]; | |
if (version == nil) | |
[TWVersion systemVersionFromGestalt]; | |
}); | |
return version; | |
} | |
+(TWVersion *)bundleVersion { | |
static TWVersion *version; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
version = [TWVersion bundleVersion:[NSBundle mainBundle]]; | |
}); | |
return version; | |
} | |
+(TWVersion *)bundleVersion:(NSBundle *)bundle { | |
NSString *marketingVersionString = [bundle infoDictionary][@"CFBundleShortVersionString"]; | |
marketingVersionString = [marketingVersionString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | |
if (marketingVersionString == nil || [marketingVersionString isEqualTo:@""]) | |
return nil; | |
TWVersion *version = TWVersion.new; | |
[version applyMarketingVersionString:marketingVersionString]; | |
[version setBuild:[bundle infoDictionary][@"CFBundleVersion"]]; | |
return version; | |
} | |
+(TWVersion *)versionWithString:(NSString *)versionString { | |
TWVersion *version = TWVersion.new; | |
[version applyMarketingVersionString:versionString]; | |
return version; | |
} | |
+(TWVersion *)systemVersionFromPList { | |
NSDictionary *versionInfo = | |
[NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]; | |
if (versionInfo == nil) | |
return nil; | |
NSString *marketingVersionString = | |
[versionInfo[@"ProductVersion"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | |
if (marketingVersionString == nil || [marketingVersionString isEqualToString:@""]) | |
return nil; | |
TWVersion *version = TWVersion.new; | |
[version applyMarketingVersionString:marketingVersionString]; | |
[version setBuild:versionInfo[@"ProductBuildVersion"]]; | |
return version; | |
} | |
+(TWVersion *)systemVersionFromGestalt { | |
int major, minor, revision; | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | |
Gestalt(gestaltSystemVersionMajor, &major); | |
Gestalt(gestaltSystemVersionMinor, &minor); | |
Gestalt(gestaltSystemVersionBugFix, &revision); | |
#pragma clang diagnostic pop | |
TWVersion *version = TWVersion.new; | |
[version setMajor:major]; | |
[version setMinor:minor]; | |
[version setRevision:revision]; | |
return version; | |
} | |
-(id)init { | |
if ((self = [super init])) { | |
_major = @(0); | |
_minor = @(0); | |
_revision = @(0); | |
} | |
return self; | |
} | |
-(void)applyMarketingVersionString:(NSString *)marketingVersionString { | |
NSArray *marketingVersionParts = [marketingVersionString componentsSeparatedByString:@"."]; | |
if (marketingVersionParts.count == 0) | |
return; | |
if (marketingVersionParts.count >= 1) | |
[self setMajor:[marketingVersionParts[0] intValue]]; | |
if (marketingVersionParts.count >= 2) | |
[self setMinor:[marketingVersionParts[1] intValue]]; | |
if (marketingVersionParts.count >= 3) | |
[self setRevision:[marketingVersionParts[2] intValue]]; | |
if (marketingVersionParts.count >= 4) | |
[self setBuild:marketingVersionParts[3]]; | |
} | |
-(void)setMajor:(int)majorVersion { | |
_major = @(majorVersion); | |
} | |
-(void)setMinor:(int)minorVersion { | |
_minor = @(minorVersion); | |
} | |
-(void)setRevision:(int)revision { | |
_revision = @(revision); | |
} | |
-(void)setBuild:(NSString *)build { | |
_build = [build stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | |
} | |
-(NSString *)marketingVersionString { | |
if (_marketingVersionString != nil) | |
return _marketingVersionString; | |
_marketingVersionString = [NSString stringWithFormat:@"%@.%@.%@", self.major, self.minor, self.revision]; | |
return _marketingVersionString; | |
} | |
-(NSString *)fullVersionString { | |
if (_fullVersionString != nil) | |
return _fullVersionString; | |
_fullVersionString = [NSString stringWithFormat:@"%@.%@.%@", self.major, self.minor, self.revision]; | |
if (self.build != nil) | |
_fullVersionString = [NSString stringWithFormat:@"%@ (%@)", _fullVersionString, self.build]; | |
return _fullVersionString; | |
} | |
-(NSComparisonResult)compare:(TWVersion *)otherVersion { | |
NSString *thisComparisonString = [self stringForComparison]; | |
NSString *otherComparisonString = [otherVersion stringForComparison]; | |
return [thisComparisonString compare:otherComparisonString options:NSNumericSearch]; | |
} | |
-(NSString *)stringForComparison { | |
NSScanner *sc = [NSScanner scannerWithString:self.build]; | |
BOOL buildIsNumeric = [sc scanFloat:NULL] && [sc isAtEnd]; | |
return buildIsNumeric | |
? [NSString stringWithFormat:@"%@.%@", self.marketingVersionString, self.build] | |
: self.marketingVersionString; | |
} | |
-(BOOL)isNewerThan:(TWVersion *)version { | |
return [self compare:version] == NSOrderedDescending; | |
} | |
-(BOOL)isOlderThan:(TWVersion *)version { | |
return [self compare:version] == NSOrderedAscending; | |
} | |
-(BOOL)isEqualTo:(TWVersion *)version { | |
return [self compare:version] == NSOrderedSame; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment