OS X version checking in Cocoa

前端 未结 5 619
猫巷女王i
猫巷女王i 2020-12-10 19:11

I am developing a Cocoa application and need to check whether the current OS X version is OS X 10.6 Snow Leopard

If the current version is Snow Leopard, I need to cl

相关标签:
5条回答
  • 2020-12-10 19:20

    There are a couple ways you could do this.

    1. 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!");
      }
    2. 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!)");
      }
    0 讨论(0)
  • 2020-12-10 19:23

    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
    }
    
    0 讨论(0)
  • 2020-12-10 19:25

    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.

    0 讨论(0)
  • 2020-12-10 19:45

    Try this source: http://cocoadevcentral.com/articles/000067.php - there is described 4 ways how to do it.

    0 讨论(0)
  • 2020-12-10 19:45

    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);
    }
    
    0 讨论(0)
提交回复
热议问题