Getting Mac Model number in Cocoa

后端 未结 4 706
再見小時候
再見小時候 2020-12-10 05:21

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 ge

4条回答
  •  萌比男神i
    2020-12-10 05:56

    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.

提交回复
热议问题