Search code examples
objective-cmacos

Troubles getting an actual macOS version


I'm using the following code:

NSOperatingSystemVersion macOsVersion()
{
    return [[NSProcessInfo processInfo] operatingSystemVersion];    
}

It's working fine when I build it on my machine - it returns 11.5.1 version. But, we use Jenkins, which is working on a remote macOS machine, and a build from Jenkins shows 10.16.0 version on my machine.

I'm not an experienced macOS developer. Am I doing something wrong? Is there a better API?


Solution

  • Thanks to CTABUYO for the code example, and mikdusan, the solution is to read contents of the /System/Library/CoreServices/.SystemVersionPlatform.plist file. It's not affected by macOS version compatibility stuff (at least for now).

    if (FileExists("/System/Library/CoreServices/.SystemVersionPlatform.plist"))
    {
        NSDictionary* systemVersion = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/.SystemVersionPlatform.plist"];
        NSString *macOSVersion = [systemVersion objectForKey:@"ProductVersion"];
        NSLog (@"productVersion =========== %@", macOSVersion);
    }
    else
    {
        NSDictionary* systemVersion = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
        NSString *macOSVersion = [systemVersion objectForKey:@"ProductVersion"];
        NSLog (@"productVersion =========== %@", macOSVersion);
    }