如何获得ios设备的硬件名称
#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,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)",