Search code examples
objective-cmacoscocoa

How to get actual name of Mac Operating System instead of version?


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];
    

Solution

  • 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 (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.