Getting Mac model number in Cocoa

I am creating an OS X application where I need to get a Mac model, for example:

iMac11,3 MacBook3,1 

And so on. Is there any class or function to get it?

+8
objective-c cocoa macos
source share
4 answers

This information is available through. sysctl :

 #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/sysctl.h> size_t len = 0; sysctlbyname("hw.model", NULL, &len, NULL, 0); if (len) { char *model = malloc(len*sizeof(char)); sysctlbyname("hw.model", model, &len, NULL, 0); printf("%s\n", model); free(model); } 
+20
source share

The API for this will be in IOKit. Looking at the IORegistryExplorer application on my laptop, I see that the first node from the root of the IOService tree is IOPlatformExpertDevice, and the entry under the "model" key is "MacBookPro6,1"

+2
source share

Without using the Cocoa direct API, you can use NSTask to execute the system_profiler command-line tool. If you run the tool like "system_profiler SPHardwareDataType", it will give you a smaller result that you can filter to extract the model identifier.

Update

I found an example with sysctl software:

 int mib[2]; size_t len = 0; char *rstring = NULL; mib[0] = CTL_HW; mib[1] = HW_MODEL; sysctl( mib, 2, NULL, &len, NULL, 0 ); rstring = malloc( len ); sysctl( mib, 2, rstring, &len, NULL, 0 ); NSLog(@"%s", rstring ); free( rstring ); rstring = NULL; 

Sourced from here .

+1
source share

I'm not sure if there is a way to get it through Cocoa, but you can use NSTask and get it through the shell.

 sysctl hw.model 
0
source share

All Articles