如何獲得ios裝置的硬體名稱
1. 如果要獲得具體是哪個裝置的第幾代產品 首先要包含
#import"sys/utsname.h"
定義為一個輔助函數:
+ (std::string) getIosDeviceNameAndGeneration
;
函數實現如下:
+ (std::string) getIosDeviceNameAndGeneration
{
structutsname systemInfo;
uname(&systemInfo);
NSLog(@"%@", [NSStringstringWithCString:systemInfo.machineencoding:NSUTF8StringEncoding]);
return std::string(systemInfo.machine);
}
utsname的結構體定義如下:
struct utsname {
char sysname[_SYS_NAMELEN]; /* [XSI] Name of OS */
char nodename[_SYS_NAMELEN]; /* [XSI] Name of this network node */
char release[_SYS_NAMELEN]; /* [XSI] Release level */
char version[_SYS_NAMELEN]; /* [XSI] Version level */
char machine[_SYS_NAMELEN]; /* [XSI] Hardware type */
};
其中machine欄位就是硬體的類型,比如 iPod2,1 等。這個表示是ipod的第二代產品。具體是什麼裝置,需要自己去解析字串。
2. 如果是只要獲得硬體的類型,比如
iPhone ,
iPod touch
iPhone Simulator等等。
+ (std::string) getIosDeviceName;
實現:
+ (std::string) getIosDeviceName
{
NSString *nsModelName=[[UIDevicecurrentDevice]model];
return [nsModelName
UTF8String];
}
machineName的值 跟 ios裝置的對照表如下: "iPhone1,1" => "iPhone 1G",
"iPhone1,2" => "iPhone 3G",
"iPhone2,1" => "iPhone 3GS",
"iPhone3,1" => "iPhone 4",
"iPhone3,2" => "iPhone 4 Verizon",
"iPhone3,3" => "iPhone 4 CDMA",
"iPhone4,1" => "iPhone 4S",
"iPod1,1" => "iPod Touch 1G",
"iPod2,1" => "iPod Touch 2G",
"iPod3,1" => "iPod Touch 3G",
"iPod4,1" => "iPod Touch 4G",
"iPad1,1" => "iPad",
"iPad2,1" => "iPad 2 (WiFi)",
"iPad2,2" => "iPad 2 (GSM)",
"iPad2,3" => "iPad 2 (CDMA)",