macoscocoacore-foundation

Obtain Model Identifier string on macOS



Every Mac has a model identifier, for example "Macmini5,1". (These are shown in the System Information app.)

enter image description here

How can I programatically obtain this model identifier string?


Solution

  • You can use sysctl

    #import <Foundation/Foundation.h>
    #import <sys/sysctl.h>
    
    NSString *ModelIdentifier()
    {
        NSString *result=@"Unknown Mac";
        size_t len=0;
        sysctlbyname("hw.model", NULL, &len, NULL, 0);
        if (len) {
            NSMutableData *data=[NSMutableData dataWithLength:len];
            sysctlbyname("hw.model", [data mutableBytes], &len, NULL, 0);
            result=[NSString stringWithUTF8String:[data bytes]];
        }
        return result;
    }