问题
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