How to get the OS X system version?

霸气de小男生 提交于 2020-01-11 15:37:36

问题


I want to get the OS X system version, such as: 10.5.4, 10.4.8, etc. I want to get it in my app, how do I do this? Thanks!


回答1:


You can read the property list at "/System/Library/CoreServices/SystemVersion.plist and extract the "ProductVersion" key, this is how the OS X installer application does it. Here's an example:

NSString *versionString;
NSDictionary * sv = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
versionString = [sv objectForKey:@"ProductVersion"];

Alternatively, the command swvers -productVersion will do the same.




回答2:


You can use Gestalt:

SInt32 version = 0;
Gestalt( gestaltSystemVersion, &version );
BOOL leopard = ( version >= 0x1050 );

if ( leopard )
{
    //draw it this way
}
else
{
    //draw it that way
}

Keep in mind if you're checking if a method is available or not, it's better to test that directly using respondsToSelector:.




回答3:


NSString *osver()
{
    SInt32 versionMajor=0, versionMinor=0, versionBugFix=0;
    Gestalt(gestaltSystemVersionMajor, &versionMajor);
    Gestalt(gestaltSystemVersionMinor, &versionMinor);
    Gestalt(gestaltSystemVersionBugFix, &versionBugFix);
    return [NSString stringWithFormat:@"%d.%d.%d", versionMajor, versionMinor, versionBugFix];
}



回答4:


-[NSProcessInfo operatingSystemVersionString] is human readable and localized. Appropriate for displaying to user or using in bug emails and such, but not appropriate for parsing.




回答5:


Again, you can use Gestalt. Look at the documentation for more information; specifically, you'll want to pass the gestaltSystemVersionMajor, gestaltSystemVersionMinor, and gestaltSystemVersionBugFix constants in the "System Version Constants" portion of the Gestalt Manager Reference documentation




回答6:


There's also a Cocoa wrapper around the Gestalt calls others have mentioned in the Google Toolbox for Mac: http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/GTMSystemVersion.h




回答7:


use this method it will return Mac OS X version

+(SInt32) OSVersion;
{
    SInt32 osxMinorVersion;
    Gestalt(gestaltSystemVersionMinor, &osxMinorVersion);
    return osxMinorVersion;
} 



回答8:


After 10_10, 8_0 were presented the better & simplest way would be
[NSProcessInfo processInfo].operatingSystemVersion
which will return
NSOperatingSystemVersion struct
with all 3 numbers.



来源:https://stackoverflow.com/questions/885911/how-to-get-the-os-x-system-version

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!