IOS代码判断设备类型

判断IOS设备类型一般会使用

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片

  1. //设备名称
  2. return [UIDevice currentDevice].name;
  3. //设备型号,只可得到是何设备,无法得到是第几代设备
  4. return [UIDevice currentDevice].model;
  5. //系统版本型号,如iPhone OS 
  6. return [UIDevice currentDevice].systemVersion;
  7. //系统版本名称,如6.1.3
  8. return [UIDevice currentDevice].systemName;

但是,这样只能区分iPhone,iPad

真正能够实现设备类型区分的方法如下

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片

  1. #import <sys/utsname.h>
  2. NSString*
  3. machineName()
  4. {
  5.     struct utsname systemInfo;
  6.     uname(&systemInfo);
  7.     return [NSString stringWithCString:systemInfo.machine
  8.                               encoding:NSUTF8StringEncoding];
  9. }

结果可能为如下的以逗号分隔的字符串

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片

  1. @”i386″      on the simulator
  2. @”iPod1,1″   on iPod Touch
  3. @”iPod2,1″   on iPod Touch Second Generation
  4. @”iPod3,1″   on iPod Touch Third Generation
  5. @”iPod4,1″   on iPod Touch Fourth Generation
  6. @”iPhone1,1″ on iPhone
  7. @”iPhone1,2″ on iPhone 3G
  8. @”iPhone2,1″ on iPhone 3GS
  9. @”iPad1,1″   on iPad
  10. @”iPad2,1″   on iPad 2
  11. @”iPad3,1″   on 3rd Generation iPad
  12. @”iPhone3,1″ on iPhone 4
  13. @”iPhone4,1″ on iPhone 4S
  14. @”iPhone5,1″ on iPhone 5 (model A1428, AT&T/Canada)
  15. @”iPhone5,2″ on iPhone 5 (model A1429, everything else)
  16. @”iPad3,4″ on 4th Generation iPad
  17. @”iPad2,5″ on iPad Mini
  18. @”iPhone5,3″ on iPhone 5c (model A1456, A1532 | GSM)
  19. @”iPhone5,4″ on iPhone 5c (model A1507, A1516, A1526 (China), A1529 | Global)
  20. @”iPhone6,1″ on iPhone 5s (model A1433, A1533 | GSM)
  21. @”iPhone6,2″ on iPhone 5s (model A1457, A1518, A1528 (China), A1530 | Global)
  22. @”iPad4,1″ on 5th Generation iPad (iPad Air) – Wifi
  23. @”iPad4,2″ on 5th Generation iPad (iPad Air) – Cellular
  24. @”iPad4,4″ on 2nd Generation iPad Mini – Wifi
  25. @”iPad4,5″ on 2nd Generation iPad Mini – Cellular

为了简化代码,网上找到了如下实现,是通过NSDictionary实现快速查询

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片

  1. #import <sys/utsname.h>
  2. NSString *machineName()
  3. {
  4.     struct utsname systemInfo;
  5.     uname(&systemInfo);
  6.     NSString *iOSDeviceModelsPath = [[NSBundle mainBundle] pathForResource:@”iOSDeviceModelMapping” ofType:@”plist”];
  7.     NSDictionary *iOSDevices = [NSDictionary dictionaryWithContentsOfFile:iOSDeviceModelsPath];
  8.     NSString* deviceModel = [NSString stringWithCString:systemInfo.machine
  9.                                                encoding:NSUTF8StringEncoding];
  10.     return [iOSDevices valueForKey:deviceModel];
  11. }

在xCode解决方案中添加以 iOSDeviceModelMapping.plist 命名的plist文件,内容如下

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
  3. <plist version=“1.0”>
  4. <dict>
  5.     <key>x86_64</key>
  6.     <string>Simulator</string>
  7.     <key>i386</key>
  8.     <string>Simulator</string>
  9.     <key>iPod1,1</key>
  10.     <string>iPod Touch 1st Gen</string>
  11.     <key>iPod2,1</key>
  12.     <string>iPod Touch 2nd Gen</string>
  13.     <key>iPod3,1</key>
  14.     <string>iPod Touch 3rd Gen</string>
  15.     <key>iPod4,1</key>
  16.     <string>iPod Touch 4th Gen</string>
  17.     <key>iPod5,1</key>
  18.     <string>iPod Touch 5th Gen</string>
  19.     <key>iPhone1,1</key>
  20.     <string>iPhone</string>
  21.     <key>iPhone1,2</key>
  22.     <string>iPhone 3G</string>
  23.     <key>iPhone2,1</key>
  24.     <string>iPhone 3GS</string>
  25.     <key>iPhone3,1</key>
  26.     <string>iPhone 4</string>
  27.     <key>iPhone3,2</key>
  28.     <string>iPhone 4</string>
  29.     <key>iPhone3,3</key>
  30.     <string>iPhone 4</string>
  31.     <key>iPhone4,1</key>
  32.     <string>iPhone 4S</string>
  33.     <key>iPhone5,1</key>
  34.     <string>iPhone 5 model A1428</string>
  35.     <key>iPhone5,2</key>
  36.     <string>iPhone 5 model A1429</string>
  37.     <key>iPhone5,3</key>
  38.     <string>iPhone 5C</string>
  39.     <key>iPhone5,4</key>
  40.     <string>iPhone 5C</string>
  41.     <key>iPhone6,1</key>
  42.     <string>iPhone 5S</string>
  43.     <key>iPhone6,2</key>
  44.     <string>iPhone 5S</string>
  45.     <key>iPad1,1</key>
  46.     <string>iPad</string>
  47.     <key>iPad2,1</key>
  48.     <string>iPad 2</string>
  49.     <key>iPad2,2</key>
  50.     <string>iPad 2</string>
  51.     <key>iPad2,3</key>
  52.     <string>iPad 2</string>
  53.     <key>iPad2,4</key>
  54.     <string>iPad 2</string>
  55.     <key>iPad3,1</key>
  56.     <string>iPad 3rd Gen</string>
  57.     <key>iPad3,2</key>
  58.     <string>iPad 3rd Gen</string>
  59.     <key>iPad3,3</key>
  60.     <string>iPad 3rd Gen</string>
  61.     <key>iPad3,4</key>
  62.     <string>iPad 4th Gen</string>
  63.     <key>iPad3,5</key>
  64.     <string>iPad 4th Gen</string>
  65.     <key>iPad3,6</key>
  66.     <string>iPad 4th Gen</string>
  67.     <key>iPad4,1</key>
  68.     <string>iPad Air</string>
  69.     <key>iPad4,2</key>
  70.     <string>iPad Air</string>
  71.     <key>iPad2,5</key>
  72.     <string>iPad Mini 1st Gen</string>
  73.     <key>iPad2,6</key>
  74.     <string>iPad Mini 1st Gen</string>
  75.     <key>iPad2,7</key>
  76.     <string>iPad Mini 1st Gen</string>
  77.     <key>iPad4,4</key>
  78.     <string>iPad Mini 2nd Gen</string>
  79.     <key>iPad4,5</key>
  80.     <string>iPad Mini 2nd Gen</string>
  81. </dict>
  82. </plist>

当MachineName方法时,NSDictionary通过匹配key,即utsname.machine的值,判断设备类型。

  1. #import <sys/utsname.h>
  2. – (NSString*) deviceName
  3. {
  4.     struct utsname systemInfo;
  5.     uname(&systemInfo);
  6.     NSString* code = [NSString stringWithCString:systemInfo.machine
  7.                                         encoding:NSUTF8StringEncoding];
  8.     static NSDictionary* deviceNamesByCode = nil;
  9.     if (!deviceNamesByCode) {
  10.         deviceNamesByCode = @{@”i386″      :@”Simulator”,
  11.                               @”iPod1,1″   :@”iPod Touch”,      // (Original)
  12.                               @”iPod2,1″   :@”iPod Touch”,      // (Second Generation)
  13.                               @”iPod3,1″   :@”iPod Touch”,      // (Third Generation)
  14.                               @”iPod4,1″   :@”iPod Touch”,      // (Fourth Generation)
  15.                               @”iPhone1,1″ :@”iPhone”,          // (Original)
  16.                               @”iPhone1,2″ :@”iPhone”,          // (3G)
  17.                               @”iPhone2,1″ :@”iPhone”,          // (3GS)
  18.                               @”iPad1,1″   :@”iPad”,            // (Original)
  19.                               @”iPad2,1″   :@”iPad 2″,          //
  20.                               @”iPad3,1″   :@”iPad”,            // (3rd Generation)
  21.                               @”iPhone3,1″ :@”iPhone 4″,        //
  22.                               @”iPhone4,1″ :@”iPhone 4S”,       //
  23.                               @”iPhone5,1″ :@”iPhone 5″,        // (model A1428, AT&T/Canada)
  24.                               @”iPhone5,2″ :@”iPhone 5″,        // (model A1429, everything else)
  25.                               @”iPad3,4″   :@”iPad”,            // (4th Generation)
  26.                               @”iPad2,5″   :@”iPad Mini”,       // (Original)
  27.                               @”iPhone5,3″ :@”iPhone 5c”,       // (model A1456, A1532 | GSM)
  28.                               @”iPhone5,4″ :@”iPhone 5c”,       // (model A1507, A1516, A1526 (China), A1529 | Global)
  29.                               @”iPhone6,1″ :@”iPhone 5s”,       // (model A1433, A1533 | GSM)
  30.                               @”iPhone6,2″ :@”iPhone 5s”,       // (model A1457, A1518, A1528 (China), A1530 | Global)
  31.                               @”iPad4,1″   :@”iPad Air”,        // 5th Generation iPad (iPad Air) – Wifi
  32.                               @”iPad4,2″   :@”iPad Air”,        // 5th Generation iPad (iPad Air) – Cellular
  33.                               @”iPad4,4″   :@”iPad Mini”,       // (2nd Generation iPad Mini – Wifi)
  34.                               @”iPad4,5″   :@”iPad Mini”        // (2nd Generation iPad Mini – Cellular)
  35.                               };
  36.     }
  37.     NSString* deviceName = [deviceNamesByCode objectForKey:code];
  38.     if (!deviceName) {
  39.         // Not found on database. At least guess main device type from string contents:
  40.         if ([deviceName rangeOfString:@”iPod”].location != NSNotFound) {
  41.             deviceName = @”iPod Touch”;
  42.         }
  43.         else if([deviceName rangeOfString:@”iPad”].location != NSNotFound) {
  44.             deviceName = @”iPad”;
  45.         }
  46.         else if([deviceName rangeOfString:@”iPhone”].location != NSNotFound){
  47.             deviceName = @”iPhone”;
  48.         }
  49.     }
  50.     return deviceName;
  51. }
  52. // (rest of class implementation omitted)
  53. @end

 

后面加个获取系统版本号的,好记心不如烂笔头

[objc]  view plain copy

  1. NSLog(@”System Name: %@”, [[UIDevice currentDevice] systemName]);  //”iPhone OS”
  2. NSLog(@”System Version: %@”, [[UIDevice currentDevice] systemVersion]);  //7.0.3