Reputation: 11790
I am able to get the version of macOS by using the code given below, however what I want is the name of operating system (using Objective-C).
NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
NSString* major = [NSString stringWithFormat:@"%d", version.majorVersion];
NSString* minor = [NSString stringWithFormat:@"%d", version.minorVersion];
NSString* patch = [NSString stringWithFormat:@"%d", version.patchVersion];
Upvotes: 5
Views: 2301
Reputation: 3453
Silly, but this should work.
public extension ProcessInfo {
func osName() -> String? {
let version = self.operatingSystemVersion
switch version.majorVersion {
case 15: return "Sequoia"
case 14: return "Sonoma"
case 13: return "Ventura"
case 12: return "Monterey"
case 11: return "Big Sur"
case 10: break
default: return "macOS \(version.majorVersion)"
}
switch version.minorVersion {
case 15: return "Catalina"
case 14: return "Mojave"
case 13: return "High Sierra"
case 12: return "Sierra"
case 11: return "El Capitan"
case 10: return "Yosemite"
case 9: return "Mavericks"
case 8: return "Mountain Lion"
case 7: return "Lion"
case 6: return "Snow Leopard"
case 5: return "Leopard"
case 4: return "Tiger"
case 3: return "Panther"
case 2: return "Jaguar"
case 1: return "Puma"
case 0: return "Cheetah"
default: return nil
}
}
Upvotes: 1
Reputation: 42149
There is no API that I know of that would produce the product name of the current OS version. Even grep
ping for the product name in system locations yields surprisingly few results, and most of those in private frameworks. The only promising non-private match I found is in the Setup Assistant.app
(edit: no longer works!), and requires a horrible kludge to extract from a longer string:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/Localizable.strings"];
NSString *productName = [dict objectForKey:@"INSTALLATION_COMPLETE"];
if (productName) {
NSRange r = [productName rangeOfString:@" has been"];
if (r.location != NSNotFound) {
productName = [productName substringToIndex:r.location];
} else {
productName = nil;
}
}
This happens to work for Yosemite and El Capitan, and produces "OS X Yosemite" and "OS X El Capitan". But even with these two versions the kludgey nature reveals itself; the El Capitan string contains non-breakable spaces…
Apart from this (or a similar kludge using other files not meant to be used this way), one can of course obtain the numeric version and match it against a list of known product names, which would be my recommended solution, perhaps with the above kludge as a fallback.
Upvotes: 3
Reputation: 11790
for the sake of one more option to figure out the OSX Name i am providing a work-around below:
// this function will return os name
-(NSString *)getOSName{
// get major version
NSString* majorVersion = [self getMajorVersion];
// get minor version
NSString* minorVersion= [self getMinorVersion];
NSString* OSName = @"";
// figure out the name using versions
if([minorVersion isEqualTo:@"11"])
{
OSName = @"El Capitan";
}
else if([minorVersion isEqualTo:@"10"])
{
OSName = @"Yosemite";
}
else if([minorVersion isEqualTo:@"9"])
{
OSName = @"Mavericks";
}
else if([minorVersion isEqualTo:@"8"])
{
OSName = @"Mountain Lion";
}
else if([minorVersion isEqualTo:@"7"])
{
OSName = @"Lion";
}
else if([minorVersion isEqualTo:@"6"])
{
OSName = @"Snow Leopard";
}
else if([minorVersion isEqualTo:@"5"])
{
OSName = @"Leopard";
}
else if([minorVersion isEqualTo:@"4"])
{
OSName = @"Tiger";
}
else if([minorVersion isEqualTo:@"3"])
{
OSName = @"Panther";
}
else if([minorVersion isEqualTo:@"2"])
{
OSName = @"Jaguar";
}
else if([minorVersion isEqualTo:@"1"])
{
OSName = @"Puma";
}
else if([minorVersion isEqualTo:@"0"])
{
OSName = @"Kodiak";
}
return OSName;
}
-(NSString*)getMajorVersion{
NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
NSString* ver = [NSString stringWithFormat:@"%d", version.majorVersion];
return ver;
}
-(NSString*)getMinorVersion{
NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
NSString* ver = [NSString stringWithFormat:@"%d", version.minorVersion];
return ver;
}
Upvotes: -3