tags:

views:

306

answers:

5

Hi all,

I am developing an application in cocoa which needs to check whether the current version is snow OS or not .If current version is snow then i need to close the application with an error alert.Please let me know how to find the current os version.

Thanks in advance

A: 

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.

stesch
This is definitely not the correct way to check the system version on Mac OS X. `Gestalt` as outlined by Dave Long is the correct way. `uname` is not guaranteed to return anything useful.
Rob Keniger
+2  A: 
Dave DeLong
Your second test will fail on OSX 11.0 (assuming there is an 11.0 someday) even though it's a newer version.
Ferruccio
@Ferruccio - good point; fixed.
Dave DeLong
"OS Ten Eleven-point-Oh" -- that'll be fun to say. ;)
mipadi
A: 

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
}
Adrian
A: 

Hi ,

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);
}
Sreelal
A: 

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

mirap