Getting the Machine Type and other Hardware details through Cocoa API

二次信任 提交于 2019-12-07 05:59:25

问题


On iOS, there is a UIDevice class which lets you get tons of great info about the device model, operating system etc…

I am looking for something equivalent on the Mac. I know about calling system_profiler from the command line, but even the mini setting takes several seconds to get any info.

I am interested in fast ways to get the machine type (Macbook Air, Mac Mini, etc…), OS version, and other quick machine details from within a Mac App. The details are going o be used as a footer on support emails sent from within the app. Is there any equivalent to UIDevice, or another fast way to get some info that could help describe a user's machine?


回答1:


#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysctl.h>

+ (NSString *)machineModel
{
    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);
        NSString *model_ns = [NSString stringWithUTF8String:model];
        free(model);
        return model_ns;
    }

    return @"Just an Apple Computer"; //in case model name can't be read
}

Swift version (just swap hw.model for hw.machine) is here https://stackoverflow.com/a/25467259/308315




回答2:


Does calling system_profiler SPHardwareDataType give you what you need? It returns very quickly and returns some basic hardware info. You can find out what other data you can request by calling system_profiler -listDataTypes. I think the other piece to your puzzle will be system_profiler SPSoftwareDataType.




回答3:


There is no built-in device class, as in iOS.

You'll need to use the Gestalt Manager to investigate the operating environment.



来源:https://stackoverflow.com/questions/8299087/getting-the-machine-type-and-other-hardware-details-through-cocoa-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!