I am developing a Cocoa application and need to check wh开发者_Python百科ether the current OS X version is OS X 10.6 Snow Leopard
If the current version is Snow Leopard, I need to close the application with an error alert.
How can I find the current OS X version?
The relevant Apple documentation can be found in Using SDK-Based Development: Determining the Version of a Framework.
They suggest either testing the existence of a specific class or method or to check the framework version number, e.g. NSAppKitVersionNumber
or NSFoundationVersionNumber
. The relevant frameworks also declare a number of constants for different os versions (NSApplication constants, Foundation Constants).
The relevant code can be as simple as:
if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
// Code for 10.6+ goes here
}
There are a couple ways you could do this.
You could check for the existence of a 10.6 only class:
Class snowLeopardOnlyClass = NSClassFromString(@"NSRunningApplication"); if (snowLeopardOnlyClass != nil) { NSLog(@"I'm running on Snow Leopard!"); }
Use a system function (like
Gestalt
) to determine the OS version:#import <CoreServices/CoreServices.h> SInt32 major = 0; SInt32 minor = 0; Gestalt(gestaltSystemVersionMajor, &major); Gestalt(gestaltSystemVersionMinor, &minor); if ((major == 10 && minor >= 6) || major >= 11) { NSLog(@"I'm running on Snow Leopard (at least!)"); }
On UNIX systems you can use the uname(3)
system call. See
$ man 3 uname
Example:
#include <stdio.h>
#include <sys/utsname.h>
int main()
{
struct utsname un;
uname(&un);
printf("sysname: %s\nnodename: %s\nrelease: %s\nversion: %s\nmachine: %s\n",
un.sysname, un.nodename, un.release, un.version, un.machine);
}
On Mac OS X 10.8.5 I get "9.8.0" as the release number. See list of releases. 10.0 is Mac OS X 10.6, 10.2.0 is Mac OS X 10.6.2.
Try this source: http://cocoadevcentral.com/articles/000067.php - there is described 4 ways how to do it.
Answering myself ,Imimplemented the alert in main.m as follows :
#ifndef NSAppKitVersionNumber10_5
#define NSAppKitVersionNumber10_5 949
#endif
int main(int argc, char *argv[])
{
SInt32 major = 0;
SInt32 minor = 0;
Gestalt(gestaltSystemVersionMajor, &major);
Gestalt(gestaltSystemVersionMinor, &minor);
if ((major == 10 && minor >= 6) || major >= 11) {
CFUserNotificationDisplayNotice(0, kCFUserNotificationCautionAlertLevel,NULL, NULL, NULL, CFSTR("Maestro"), CFSTR("This version is not compatible."), CFSTR("Ok"));
return 0;
}
return NSApplicationMain(argc, (const char **) argv);
}
精彩评论