通过Cocoa API获取机器类型和其他硬件详细信息

con*_*are 5 macos profiler cocoa objective-c

在iOS上,有一个UIDevice类,可以让您获得有关设备模型,操作系统等的大量信息......

我在Mac上寻找相同的东西.我知道system_profiler从命令行调用,但即使mini设置需要几秒钟来获取任何信息.

我对从Mac应用程序中获取机器类型(Macbook Air,Mac Mini等),操作系统版本以及其他快速机器详细信息的快速方法感兴趣.详细信息将用作从应用程序内发送的支持电子邮件的页脚.有没有相当于UIDevice,或另一种快速的方法来获得一些可以帮助描述用户的机器的信息?

erk*_*diz 21

斯威夫特版本(只是换hw.modelhw.machine),在这里/sf/answers/1782708161/


#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
}
Run Code Online (Sandbox Code Playgroud)


Mac*_*ade 0

与 iOS 一样,没有内置设备类。

您需要使用Gestalt Manager来调查操作环境。