Getting the Machine Type and other Hardware details through Cocoa API

感情迁移 提交于 2019-12-05 12:16:21
#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

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.

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

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

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