判断IOS设备类型一般会使用
- //设备名称
- return [UIDevice currentDevice].name;
- //设备型号,只可得到是何设备,无法得到是第几代设备
- return [UIDevice currentDevice].model;
- //系统版本型号,如iPhone OS
- return [UIDevice currentDevice].systemVersion;
- //系统版本名称,如6.1.3
- return [UIDevice currentDevice].systemName;
但是,这样只能区分iPhone,iPad
真正能够实现设备类型区分的方法如下
- #import <sys/utsname.h>
- NSString*
- machineName()
- {
- struct utsname systemInfo;
- uname(&systemInfo);
- return [NSString stringWithCString:systemInfo.machine
- encoding:NSUTF8StringEncoding];
- }
结果可能为如下的以逗号分隔的字符串
- @"i386" on the simulator
- @"iPod1,1" on iPod Touch
- @"iPod2,1" on iPod Touch Second Generation
- @"iPod3,1" on iPod Touch Third Generation
- @"iPod4,1" on iPod Touch Fourth Generation
- @"iPhone1,1" on iPhone
- @"iPhone1,2" on iPhone 3G
- @"iPhone2,1" on iPhone 3GS
- @"iPad1,1" on iPad
- @"iPad2,1" on iPad 2
- @"iPad3,1" on 3rd Generation iPad
- @"iPhone3,1" on iPhone 4
- @"iPhone4,1" on iPhone 4S
- @"iPhone5,1" on iPhone 5 (model A1428, AT&T/Canada)
- @"iPhone5,2" on iPhone 5 (model A1429, everything else)
- @"iPad3,4" on 4th Generation iPad
- @"iPad2,5" on iPad Mini
- @"iPhone5,3" on iPhone 5c (model A1456, A1532 | GSM)
- @"iPhone5,4" on iPhone 5c (model A1507, A1516, A1526 (China), A1529 | Global)
- @"iPhone6,1" on iPhone 5s (model A1433, A1533 | GSM)
- @"iPhone6,2" on iPhone 5s (model A1457, A1518, A1528 (China), A1530 | Global)
- @"iPad4,1" on 5th Generation iPad (iPad Air) - Wifi
- @"iPad4,2" on 5th Generation iPad (iPad Air) - Cellular
- @"iPad4,4" on 2nd Generation iPad Mini - Wifi
- @"iPad4,5" on 2nd Generation iPad Mini - Cellular
为了简化代码,网上找到了如下实现,是通过NSDictionary实现快速查询
- #import <sys/utsname.h>
- NSString *machineName()
- {
- struct utsname systemInfo;
- uname(&systemInfo);
- NSString *iOSDeviceModelsPath = [[NSBundle mainBundle] pathForResource:@"iOSDeviceModelMapping" ofType:@"plist"];
- NSDictionary *iOSDevices = [NSDictionary dictionaryWithContentsOfFile:iOSDeviceModelsPath];
- NSString* deviceModel = [NSString stringWithCString:systemInfo.machine
- encoding:NSUTF8StringEncoding];
- return [iOSDevices valueForKey:deviceModel];
- }
在xCode解决方案中添加以 iOSDeviceModelMapping.plist 命名的plist文件,内容如下
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <dict>
- <key>x86_64</key>
- <string>Simulator</string>
- <key>i386</key>
- <string>Simulator</string>
- <key>iPod1,1</key>
- <string>iPod Touch 1st Gen</string>
- <key>iPod2,1</key>
- <string>iPod Touch 2nd Gen</string>
- <key>iPod3,1</key>
- <string>iPod Touch 3rd Gen</string>
- <key>iPod4,1</key>
- <string>iPod Touch 4th Gen</string>
- <key>iPod5,1</key>
- <string>iPod Touch 5th Gen</string>
- <key>iPhone1,1</key>
- <string>iPhone</string>
- <key>iPhone1,2</key>
- <string>iPhone 3G</string>
- <key>iPhone2,1</key>
- <string>iPhone 3GS</string>
- <key>iPhone3,1</key>
- <string>iPhone 4</string>
- <key>iPhone3,2</key>
- <string>iPhone 4</string>
- <key>iPhone3,3</key>
- <string>iPhone 4</string>
- <key>iPhone4,1</key>
- <string>iPhone 4S</string>
- <key>iPhone5,1</key>
- <string>iPhone 5 model A1428</string>
- <key>iPhone5,2</key>
- <string>iPhone 5 model A1429</string>
- <key>iPhone5,3</key>
- <string>iPhone 5C</string>
- <key>iPhone5,4</key>
- <string>iPhone 5C</string>
- <key>iPhone6,1</key>
- <string>iPhone 5S</string>
- <key>iPhone6,2</key>
- <string>iPhone 5S</string>
- <key>iPad1,1</key>
- <string>iPad</string>
- <key>iPad2,1</key>
- <string>iPad 2</string>
- <key>iPad2,2</key>
- <string>iPad 2</string>
- <key>iPad2,3</key>
- <string>iPad 2</string>
- <key>iPad2,4</key>
- <string>iPad 2</string>
- <key>iPad3,1</key>
- <string>iPad 3rd Gen</string>
- <key>iPad3,2</key>
- <string>iPad 3rd Gen</string>
- <key>iPad3,3</key>
- <string>iPad 3rd Gen</string>
- <key>iPad3,4</key>
- <string>iPad 4th Gen</string>
- <key>iPad3,5</key>
- <string>iPad 4th Gen</string>
- <key>iPad3,6</key>
- <string>iPad 4th Gen</string>
- <key>iPad4,1</key>
- <string>iPad Air</string>
- <key>iPad4,2</key>
- <string>iPad Air</string>
- <key>iPad2,5</key>
- <string>iPad Mini 1st Gen</string>
- <key>iPad2,6</key>
- <string>iPad Mini 1st Gen</string>
- <key>iPad2,7</key>
- <string>iPad Mini 1st Gen</string>
- <key>iPad4,4</key>
- <string>iPad Mini 2nd Gen</string>
- <key>iPad4,5</key>
- <string>iPad Mini 2nd Gen</string>
- </dict>
- </plist>
当MachineName方法时,NSDictionary通过匹配key,即utsname.machine的值,判断设备类型。
网上找到的类似的解决方法
- #import <sys/utsname.h>
- - (NSString*) deviceName
- {
- struct utsname systemInfo;
- uname(&systemInfo);
- NSString* code = [NSString stringWithCString:systemInfo.machine
- encoding:NSUTF8StringEncoding];
- static NSDictionary* deviceNamesByCode = nil;
- if (!deviceNamesByCode) {
- deviceNamesByCode = @{@"i386" :@"Simulator",
- @"iPod1,1" :@"iPod Touch", // (Original)
- @"iPod2,1" :@"iPod Touch", // (Second Generation)
- @"iPod3,1" :@"iPod Touch", // (Third Generation)
- @"iPod4,1" :@"iPod Touch", // (Fourth Generation)
- @"iPhone1,1" :@"iPhone", // (Original)
- @"iPhone1,2" :@"iPhone", // (3G)
- @"iPhone2,1" :@"iPhone", // (3GS)
- @"iPad1,1" :@"iPad", // (Original)
- @"iPad2,1" :@"iPad 2", //
- @"iPad3,1" :@"iPad", // (3rd Generation)
- @"iPhone3,1" :@"iPhone 4", //
- @"iPhone4,1" :@"iPhone 4S", //
- @"iPhone5,1" :@"iPhone 5", // (model A1428, AT&T/Canada)
- @"iPhone5,2" :@"iPhone 5", // (model A1429, everything else)
- @"iPad3,4" :@"iPad", // (4th Generation)
- @"iPad2,5" :@"iPad Mini", // (Original)
- @"iPhone5,3" :@"iPhone 5c", // (model A1456, A1532 | GSM)
- @"iPhone5,4" :@"iPhone 5c", // (model A1507, A1516, A1526 (China), A1529 | Global)
- @"iPhone6,1" :@"iPhone 5s", // (model A1433, A1533 | GSM)
- @"iPhone6,2" :@"iPhone 5s", // (model A1457, A1518, A1528 (China), A1530 | Global)
- @"iPad4,1" :@"iPad Air", // 5th Generation iPad (iPad Air) - Wifi
- @"iPad4,2" :@"iPad Air", // 5th Generation iPad (iPad Air) - Cellular
- @"iPad4,4" :@"iPad Mini", // (2nd Generation iPad Mini - Wifi)
- @"iPad4,5" :@"iPad Mini" // (2nd Generation iPad Mini - Cellular)
- };
- }
- NSString* deviceName = [deviceNamesByCode objectForKey:code];
- if (!deviceName) {
- // Not found on database. At least guess main device type from string contents:
- if ([deviceName rangeOfString:@"iPod"].location != NSNotFound) {
- deviceName = @"iPod Touch";
- }
- else if([deviceName rangeOfString:@"iPad"].location != NSNotFound) {
- deviceName = @"iPad";
- }
- else if([deviceName rangeOfString:@"iPhone"].location != NSNotFound){
- deviceName = @"iPhone";
- }
- }
- return deviceName;
- }
- // (rest of class implementation omitted)
- @end
后面加个获取系统版本号的,好记心不如烂笔头
- NSLog(@"System Name: %@", [[UIDevice currentDevice] systemName]); //"iPhone OS"
- NSLog(@"System Version: %@", [[UIDevice currentDevice] systemVersion]); //7.0.3