Amazon

Monday, November 12, 2007

Programmatically Determine Mac OS X Version Number

So when I decided to modify my Freeciv launcher to behave according to the version of Mac OS X it is running on top of, I needed a programmatic way to get at the OS version at run-time.

I read in Xcode 2.x's documentation that the best way seems to get it out of the file /System/Library/CoreServices/SystemVersion.plist. Even though it is a plain-text XML file, I didn't just parse it via brute-force and get at the number. So I had to parse it properly. It turns out to be pretty easy.

NSString * text = [NSString stringWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
NSDictionary * plist = [text propertyListFromStringsFileFormat];
NSString * productVersion = [plist valueForKey:@"ProductVersion"];

Three lines of code, and I can get at the strings such as 10.4.9 for Tiger and 10.5 for Leopard. Simple. I like it.

No comments: