Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine the OS version at runtime in OS X or iOS (without using Gestalt)?

The Gestalt() function located in CarbonCore/OSUtils.h has been deprecated as of OS X 10.8 Mountain Lion.

I often use this function to test the version of the OS X operating system at runtime (see the toy example below).

What other API could be used to check the OS X operating system version at runtime in a Cocoa application?

int main() {     SInt32 versMaj, versMin, versBugFix;     Gestalt(gestaltSystemVersionMajor, &versMaj);     Gestalt(gestaltSystemVersionMinor, &versMin);     Gestalt(gestaltSystemVersionBugFix, &versBugFix);      printf("OS X Version: %d.%d.%d\n", versMaj, versMin, versBugFix); } 
like image 819
Todd Ditchendorf Avatar asked Jun 17 '12 16:06

Todd Ditchendorf


People also ask

How do I know the version of my macOS?

Which macOS version is installed? From the Apple menu  in the corner of your screen, choose About This Mac. You should see the macOS name, such as macOS Monterey or macOS Big Sur, followed by its version number. If you need to know the build number as well, click the version number to see it.

What is the version number and name of the release of OS used on the IMAC?

Version 10.15: "Catalina" It was released on October 7, 2019.


2 Answers

On OS X 10.10 (and iOS 8.0), you can use [[NSProcessInfo processInfo] operatingSystemVersion] which returns a NSOperatingSystemVersion struct, defined as

typedef struct {     NSInteger majorVersion;     NSInteger minorVersion;     NSInteger patchVersion; } NSOperatingSystemVersion; 

There is also a method in NSProcessInfo that will do the comparison for you:

- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version 

Beware, although documented to be available in OS X 10.10 and later, both operatingSystemVersion and isOperatingSystemAtLeastVersion: exist on OS X 10.9 (probably 10.9.2) and work as expected. It means that you must not test if NSProcessInfo responds to these selectors to check if you are running on OS X 10.9 or 10.10.

On iOS, these methods are effectively only available since iOS 8.0.

like image 170
0xced Avatar answered Oct 06 '22 12:10

0xced


On the command line:

$ sysctl kern.osrelease kern.osrelease: 12.0.0 $ sysctl kern.osversion kern.osversion: 12A269 

Programmatically:

#include <errno.h> #include <sys/sysctl.h>  char str[256]; size_t size = sizeof(str); int ret = sysctlbyname("kern.osrelease", str, &size, NULL, 0); 

Darwin version to OS X release:

17.x.x. macOS 10.13.x High Sierra 16.x.x  macOS 10.12.x Sierra 15.x.x  OS X  10.11.x El Capitan 14.x.x  OS X  10.10.x Yosemite 13.x.x  OS X  10.9.x  Mavericks 12.x.x  OS X  10.8.x  Mountain Lion 11.x.x  OS X  10.7.x  Lion 10.x.x  OS X  10.6.x  Snow Leopard  9.x.x  OS X  10.5.x  Leopard  8.x.x  OS X  10.4.x  Tiger  7.x.x  OS X  10.3.x  Panther  6.x.x  OS X  10.2.x  Jaguar  5.x    OS X  10.1.x  Puma 

A Sample to get and test versions :

#include <string.h> #include <stdio.h> #include <sys/sysctl.h>  /* kernel version as major minor component*/ struct kern {     short int version[3]; };  /* return the kernel version */ void GetKernelVersion(struct kern *k) {    static short int version_[3] = {0};    if (!version_[0]) {       // just in case it fails someday       version_[0] = version_[1] = version_[2] = -1;       char str[256] = {0};       size_t size = sizeof(str);       int ret = sysctlbyname("kern.osrelease", str, &size, NULL, 0);       if (ret == 0) sscanf(str, "%hd.%hd.%hd", &version_[0], &version_[1], &version_[2]);     }     memcpy(k->version, version_, sizeof(version_)); }  /* compare os version with a specific one 0 is equal negative value if the installed version is less positive value if the installed version is more */ int CompareKernelVersion(short int major, short int minor, short int component) {     struct kern k;     GetKernelVersion(&k);     if ( k.version[0] !=  major) return major - k.version[0];     if ( k.version[1] !=  minor) return minor - k.version[1];     if ( k.version[2] !=  component) return component - k.version[2];     return 0; }  int main() {    struct kern kern;    GetKernelVersion(&kern);    printf("%hd %hd %hd\n", kern.version[0], kern.version[1], kern.version[2]);     printf("up: %d %d eq %d %d low %d %d\n",         CompareKernelVersion(17, 0, 0), CompareKernelVersion(16, 3, 0),         CompareKernelVersion(17, 3, 0), CompareKernelVersion(17,3,0),         CompareKernelVersion(17,5,0), CompareKernelVersion(18,3,0));   } 

Result on my machine macOs High Sierra 10.13.2

17 3 0 up: -3 -1 eq 0 0 low 2 1 
like image 43
Variable Length Coder Avatar answered Oct 06 '22 12:10

Variable Length Coder