Skip to content

Instantly share code, notes, and snippets.

@summic
Created November 21, 2012 03:14
Show Gist options
  • Save summic/4122788 to your computer and use it in GitHub Desktop.
Save summic/4122788 to your computer and use it in GitHub Desktop.
获取硬件信息
#import <sys/sysctl.h>
+ (NSString *)hardwareModel
{
static NSString *hardwareModel = nil;
if (!hardwareModel) {
char buffer[128];
size_t length = sizeof(buffer);
if (sysctlbyname("hw.model", &buffer, &length, NULL, 0) == 0) {
hardwareModel = [[NSString allocWithZone:NULL] initWithCString:buffer encoding:NSASCIIStringEncoding];
}
if (!hardwareModel || [hardwareModel length] == 0) {
hardwareModel = @"Unknown";
}
}
return hardwareModel;
}
+ (NSString *)computerModel
{
static NSString *computerModel = nil;
if (!computerModel) {
NSString *path, *hardwareModel = [self hardwareModel];
if ((path = [[NSBundle mainBundle] pathForResource:@"Macintosh" ofType:@"dict"])) {
computerModel = [[[NSDictionary dictionaryWithContentsOfFile:path] objectForKey:hardwareModel] copy];
}
if (!computerModel) {
char buffer[128];
size_t length = sizeof(buffer);
if (sysctlbyname("hw.machine", &buffer, &length, NULL, 0) == 0) {
computerModel = [[NSString allocWithZone:NULL] initWithCString:buffer encoding:NSASCIIStringEncoding];
}
}
if (!computerModel || [computerModel length] == 0) {
computerModel = [[NSString allocWithZone:NULL] initWithFormat:@"%@ computer model", hardwareModel];
}
}
return computerModel;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment