2

I am able to get the version of OSX by using the code given below however what I want is the name of operating system (using Objective C).

Thanks in advance!!!

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];
Vikas Bansal
  • 10,662
  • 14
  • 58
  • 100
  • Hmm, might not be solvable without hard coding the mapping from version numbers to names (and keeping it up to date as new versions are released) or looking into some system files (which are unlikely to be guaranteed to work across versions). – Arkku Jul 27 '15 at 16:24

3 Answers3

3

There is no API that I know of that would produce the product name of the current OS version. Even grepping 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, 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.

Arkku
  • 41,011
  • 10
  • 62
  • 84
0

Silly, but this should work.

public extension ProcessInfo {
func osName() -> String? {
    let version = self.operatingSystemVersion
    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 "Kodiak"
        default: return nil
    }
}
Ji Fang
  • 3,288
  • 1
  • 21
  • 18
-3

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;

}

Vikas Bansal
  • 10,662
  • 14
  • 58
  • 100
  • Umm, why do you first convert the integer to a string inside `getMajorVersion` and `getMinorVersion`, when you then end up having to do a bunch of overly verbose and inefficient `isEqualTo:` comparisons instead of being able to `switch` the integers? Also, you repeatedly do the comparison against `@"10"`; as this covers almost all of your cases you could just check once and nest the minor version checks inside the then-branch. – Arkku Aug 08 '15 at 00:34
  • Now you've made it incorrect in case multiple releases were to have the same minor version (currently they don't, but they well might if the major version is ever bumped). And you're still unnecessarily comparing strings rather than integers. – Arkku Aug 09 '15 at 15:51
  • (Also, the default value for an unrecognised OS version probably ought to be `nil` rather than the empty string, so the caller can easily check for it, and the naming convention for Objective-C is to not have the “get” prefix, i.e., `operatingSystemReleaseName` and `operatingSystemMinorVersionString` would be more consistent with that, although the latter, as said before, is unnecessary because you already get the much more usable `operatingSystemVersion` from `processInfo`.) – Arkku Aug 09 '15 at 16:04
  • `operatingSystemVersion` is 10.10+, so the majority of this code is useless... However, I'm with @Arkku that this code is passing from integer to strings without any need for it, a `switch` statement would've done it better! – gog Jun 13 '19 at 14:08
  • Mac OS X 10.0 was code-named Cheetah, not Kodiak. (Kodiak is a bear; the code-names of the early versions were all big cats.) – Cody Gray - on strike Jan 13 '23 at 00:35