11

I'm making a OS X app where I need to get the Mac model, for example:

iMac11,3
MacBook3,1

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

一二三
  • 21,059
  • 11
  • 65
  • 74
pmerino
  • 5,900
  • 11
  • 57
  • 76

4 Answers4

27

This information is available via. 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);
}
一二三
  • 21,059
  • 11
  • 65
  • 74
2

The API for that would be in the IOKit. Looking in the IORegistryExplorer app on my laptop, I see that the first node from the root of the IOService tree is an IOPlatformExpertDevice, with a entry under the key "model" equal to "MacBookPro6,1"

NSResponder
  • 16,861
  • 7
  • 32
  • 46
0

While not using a direct Cocoa API, you could use NSTask to execute the "system_profiler" command line tool. If you execute the tool as: "system_profiler SPHardwareDataType" it will give you a smaller output which could be filtered to extract the model identifier.

Update

I found an example using sysctl programmatically:

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.

Maurice Kelly
  • 1,462
  • 12
  • 15
  • Alternatively some additional searching suggests using _Gestalt_ and `gestaltUserVisibleMachineName` - have a look at the [Gestalt documentation](http://developer.apple.com/library/mac/#documentation/Carbon/Reference/Gestalt_Manager/Reference/reference.html). – Maurice Kelly Sep 11 '11 at 11:45
0

I'm not sure if there is an exact way of getting it through Cocoa, but you can use NSTask and get this through shell.


sysctl hw.model
TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71