tags:

views:

658

answers:

4

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!

+7  A: 

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:.

Marc Charbonneau
Note that gestaltSystemVersion is discouraged since 10.4 in favor of gestaltSystemVersionMajor, gestaltSystemVersionMinor, and gestaltSystemVersionBugFix, due to the 1-digit limit. 10.4.10 and .11, for example, are impossible to detect using your code.
smorgan
+2  A: 

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

BJ Homer
A: 

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

smorgan
Meh, seems like more trouble than it's worth for a simple check. Maybe if I were already using GTM, but it's simpler to just check `gestaltSystemVersionMinor`.
Quinn Taylor
A: 

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.

altan
That works, but it incurs disk I/O and is unnecessary. Gestalt is a much better (and faster) alternative.
Quinn Taylor