Created
November 21, 2012 03:14
-
-
Save summic/4122788 to your computer and use it in GitHub Desktop.
获取硬件信息
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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