2011-09-11 64 views
8

我正在做一個OS X的應用程序,我需要得到蘋果的模式,例如:獲取的Mac型號可可

iMac11,3 
MacBook3,1 

等。有沒有什麼課程或功能可以得到它?

回答

20

此信息可通過。 sysctl

#include <stdlib.h> 
#include <stdio.h> 
#include <sys/types.h> 
#include <sys/sysctl.h> 

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); 
    printf("%s\n", model); 
    free(model); 
} 
2

該API的API將在IOKit中。看在我的筆記本電腦IORegistryExplorer應用程序,我看到一個從IOService樹的根的第一個節點是IOPlatformExpertDevice,與鍵「模式」等於「MacBookPro6,1」

1

下一個條目雖然沒有使用直接使用Cocoa API,可以使用NSTask來執行「system_profiler」命令行工具。如果你執行這個工具:「system_profiler SPHardwareDataType」它會給你一個較小的輸出,它可以被過濾來提取模型標識符。

更新

我發現了一個例子使用的sysctl編程:

int mib[2]; 
size_t len = 0; 
char *rstring = NULL; 

mib[0] = CTL_HW; 
mib[1] = HW_MODEL; 
sysctl(mib, 2, NULL, &len, NULL, 0); 
rstring = malloc(len); 
sysctl(mib, 2, rstring, &len, NULL, 0); 
NSLog(@"%s", rstring); 
free(rstring); 
rstring = NULL; 

來源from here

+0

可選地一些額外的搜索建議使用_Gestalt_和'gestaltUserVisibleMachineName' - 看看在[完形文檔】(http://developer.apple.com/library/mac/#documentation/Carbon/Reference/Gestalt_Manager /Reference/reference.html)。 –

0

我不知道是否有得到它通過可可的確切方式,但你可以使用NSTask和通過shell得到這個。


sysctl hw.model