How to get device make and model on iOS?
我想知道是否可以确定当前设备是哪种类型的iPhone?我知道有可能让模型通过
好的,让我知道,谢谢!
请尝试此库:http://github.com/erica/uidevice-extension/(作者:erica sadun)。(图书馆有7-8年的历史,因此已过时)
(样本代码):
1 2 | [[UIDevice currentDevice] platformType] // ex: UIDevice4GiPhone [[UIDevice currentDevice] platformString] // ex: @"iPhone 4G" |
或者可以使用此方法:
您可以使用sys/utsname.h中的uname获取设备型号。例如:
Objtovi-C
1 2 3 4 5 6 7 8 9 10 | #import <sys/utsname.h> // import it in your header or implementation file. NSString* deviceName() { struct utsname systemInfo; uname(&systemInfo); return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; } |
斯威夫特3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | extension UIDevice { var modelName: String { var systemInfo = utsname() uname(&systemInfo) let machineMirror = Mirror(reflecting: systemInfo.machine) let identifier = machineMirror.children.reduce("") { identifier, element in guard let value = element.value as? Int8, value != 0 else { return identifier } return identifier + String(UnicodeScalar(UInt8(value))) } return identifier } } print(UIDevice.current.modelName) |
结果应该是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | //Simultor @"i386" on 32-bit Simulator @"x86_64" on 64-bit Simulator //iPhone @"iPhone1,1" on iPhone @"iPhone1,2" on iPhone 3G @"iPhone2,1" on iPhone 3GS @"iPhone3,1" on iPhone 4 (GSM) @"iPhone3,3" on iPhone 4 (CDMA/Verizon/Sprint) @"iPhone4,1" on iPhone 4S @"iPhone5,1" on iPhone 5 (model A1428, AT&T/Canada) @"iPhone5,2" on iPhone 5 (model A1429, everything else) @"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) @"iPhone7,1" on iPhone 6 Plus @"iPhone7,2" on iPhone 6 @"iPhone8,1" on iPhone 6S @"iPhone8,2" on iPhone 6S Plus @"iPhone8,4" on iPhone SE @"iPhone9,1" on iPhone 7 (CDMA) @"iPhone9,3" on iPhone 7 (GSM) @"iPhone9,2" on iPhone 7 Plus (CDMA) @"iPhone9,4" on iPhone 7 Plus (GSM) @"iPhone10,1" on iPhone 8 (CDMA) @"iPhone10,4" on iPhone 8 (GSM) @"iPhone10,2" on iPhone 8 Plus (CDMA) @"iPhone10,5" on iPhone 8 Plus (GSM) @"iPhone10,3" on iPhone X (CDMA) @"iPhone10,6" on iPhone X (GSM) @"iPhone11,2" on iPhone XS @"iPhone11,4" on iPhone XS Max @"iPhone11,6" on iPhone XS Max China @"iPhone11,8" on iPhone XR //iPad 1 @"iPad1,1" on iPad - Wifi (model A1219) @"iPad1,1" on iPad - Wifi + Cellular (model A1337) //iPad 2 @"iPad2,1" - Wifi (model A1395) @"iPad2,2" - GSM (model A1396) @"iPad2,3" - 3G (model A1397) @"iPad2,4" - Wifi (model A1395) // iPad Mini @"iPad2,5" - Wifi (model A1432) @"iPad2,6" - Wifi + Cellular (model A1454) @"iPad2,7" - Wifi + Cellular (model A1455) //iPad 3 @"iPad3,1" - Wifi (model A1416) @"iPad3,2" - Wifi + Cellular (model A1403) @"iPad3,3" - Wifi + Cellular (model A1430) //iPad 4 @"iPad3,4" - Wifi (model A1458) @"iPad3,5" - Wifi + Cellular (model A1459) @"iPad3,6" - Wifi + Cellular (model A1460) //iPad AIR @"iPad4,1" - Wifi (model A1474) @"iPad4,2" - Wifi + Cellular (model A1475) @"iPad4,3" - Wifi + Cellular (model A1476) // iPad Mini 2 @"iPad4,4" - Wifi (model A1489) @"iPad4,5" - Wifi + Cellular (model A1490) @"iPad4,6" - Wifi + Cellular (model A1491) // iPad Mini 3 @"iPad4,7" - Wifi (model A1599) @"iPad4,8" - Wifi + Cellular (model A1600) @"iPad4,9" - Wifi + Cellular (model A1601) // iPad Mini 4 @"iPad5,1" - Wifi (model A1538) @"iPad5,2" - Wifi + Cellular (model A1550) //iPad AIR 2 @"iPad5,3" - Wifi (model A1566) @"iPad5,4" - Wifi + Cellular (model A1567) // iPad PRO 9.7" @"iPad6,3" - Wifi (model A1673) @"iPad6,4" - Wifi + Cellular (model A1674) @"iPad6,4" - Wifi + Cellular (model A1675) //iPad PRO 12.9" @"iPad6,7" - Wifi (model A1584) @"iPad6,8" - Wifi + Cellular (model A1652) //iPad (5th generation) @"iPad6,11" - Wifi (model A1822) @"iPad6,12" - Wifi + Cellular (model A1823) //iPad PRO 12.9" (2nd Gen) @"iPad7,1" - Wifi (model A1670) @"iPad7,2" - Wifi + Cellular (model A1671) @"iPad7,2" - Wifi + Cellular (model A1821) //iPad PRO 10.5" @"iPad7,3" - Wifi (model A1701) @"iPad7,4" - Wifi + Cellular (model A1709) //iPod Touch @"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 @"iPod7,1" on iPod Touch 6th Generation // Apple Watch @"Watch1,1" on Apple Watch 38mm case @"Watch1,2" on Apple Watch 38mm case @"Watch2,6" on Apple Watch Series 1 38mm case @"Watch2,7" on Apple Watch Series 1 42mm case @"Watch2,3" on Apple Watch Series 2 38mm case @"Watch2,4" on Apple Watch Series 2 42mm case @"Watch3,1" on Apple Watch Series 3 38mm case (GPS+Cellular) @"Watch3,2" on Apple Watch Series 3 42mm case (GPS+Cellular) @"Watch3,3" on Apple Watch Series 3 38mm case (GPS) @"Watch3,4" on Apple Watch Series 3 42mm case (GPS) @"Watch4,1" on Apple Watch Series 4 40mm case (GPS) @"Watch4,2" on Apple Watch Series 4 44mm case (GPS) @"Watch4,3" on Apple Watch Series 4 40mm case (GPS+Cellular) @"Watch4,4" on Apple Watch Series 4 44mm case (GPS+Cellular) |
在上面对ohmee的答案进行扩展后,我添加了一些故障保护,以支持列表中未包含的未来设备:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #import <sys/utsname.h> #import"MyClass.h" @implementation MyClass { //(your private ivars) } - (NSString*) deviceName { struct utsname systemInfo; uname(&systemInfo); NSString* code = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; static NSDictionary* deviceNamesByCode = nil; if (!deviceNamesByCode) { deviceNamesByCode = @{@"i386" : @"Simulator", @"x86_64" : @"Simulator", @"iPod1,1" : @"iPod Touch", // (Original) @"iPod2,1" : @"iPod Touch", // (Second Generation) @"iPod3,1" : @"iPod Touch", // (Third Generation) @"iPod4,1" : @"iPod Touch", // (Fourth Generation) @"iPod7,1" : @"iPod Touch", // (6th 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", // (GSM) @"iPhone3,3" : @"iPhone 4", // (CDMA/Verizon/Sprint) @"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) @"iPhone7,1" : @"iPhone 6 Plus", // @"iPhone7,2" : @"iPhone 6", // @"iPhone8,1" : @"iPhone 6S", // @"iPhone8,2" : @"iPhone 6S Plus", // @"iPhone8,4" : @"iPhone SE", // @"iPhone9,1" : @"iPhone 7", // @"iPhone9,3" : @"iPhone 7", // @"iPhone9,2" : @"iPhone 7 Plus", // @"iPhone9,4" : @"iPhone 7 Plus", // @"iPhone10,1": @"iPhone 8", // CDMA @"iPhone10,4": @"iPhone 8", // GSM @"iPhone10,2": @"iPhone 8 Plus", // CDMA @"iPhone10,5": @"iPhone 8 Plus", // GSM @"iPhone10,3": @"iPhone X", // CDMA @"iPhone10,6": @"iPhone X", // GSM @"iPhone11,2": @"iPhone XS", // @"iPhone11,4": @"iPhone XS Max", // @"iPhone11,6": @"iPhone XS Max", // China @"iPhone11,8": @"iPhone XR", // @"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) @"iPad4,7" : @"iPad Mini", // (3rd Generation iPad Mini - Wifi (model A1599)) @"iPad6,7" : @"iPad Pro (12.9")", // iPad Pro 12.9 inches - (model A1584) @"iPad6,8" : @"iPad Pro (12.9")", // iPad Pro 12.9 inches - (model A1652) @"iPad6,3" : @"iPad Pro (9.7")", // iPad Pro 9.7 inches - (model A1673) @"iPad6,4" : @"iPad Pro (9.7")" // iPad Pro 9.7 inches - (models A1674 and A1675) }; } NSString* deviceName = [deviceNamesByCode objectForKey:code]; if (!deviceName) { // Not found on database. At least guess main device type from string contents: if ([code rangeOfString:@"iPod"].location != NSNotFound) { deviceName = @"iPod Touch"; } else if([code rangeOfString:@"iPad"].location != NSNotFound) { deviceName = @"iPad"; } else if([code rangeOfString:@"iPhone"].location != NSNotFound){ deviceName = @"iPhone"; } else { deviceName = @"Unknown"; } } return deviceName; } // (rest of class implementation omitted) @end |
我还省略了详细信息(例如"A1507、A1516、A1526(中国)、A1529 Global"),并将其放在注释中,以防您希望将其用作面向用户的字符串,而不是将其吓坏。
编辑:这个答案提供了一个使用Swift2的类似实现。
编辑2:我刚添加了iPad Pro型号(两种尺寸)。可在iPhone wiki中找到型号等以供将来参考。
编辑3:添加对iPhone XS、iPhone XS Max和iPhone XR的支持。
我已经创建了plist文件来帮助您获得每个设备的完整名称(源代码在我的答案的末尾)
根据Ohmee的答案,您可以这样使用它:
斯威夫特4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | static func deviceName() -> String { var systemInfo = utsname() uname(&systemInfo) guard let iOSDeviceModelsPath = Bundle.main.path(forResource:"iOSDeviceModelMapping", ofType:"plist") else { return"" } guard let iOSDevices = NSDictionary(contentsOfFile: iOSDeviceModelsPath) else { return"" } let machineMirror = Mirror(reflecting: systemInfo.machine) let identifier = machineMirror.children.reduce("") { identifier, element in guard let value = element.value as? Int8, value != 0 else { return identifier } return identifier + String(UnicodeScalar(UInt8(value))) } return iOSDevices.value(forKey: identifier) as! String } |
不要忘记在桥接头中添加
目标C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #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]; } |
plist文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | <?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>iPod7,1</key> <string>iPod Touch 6th 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>iPhone7,2</key> <string>iPhone 6</string> <key>iPhone7,1</key> <string>iPhone 6 Plus</string> <key>iPhone8,1</key> <string>iPhone 6S</string> <key>iPhone8,2</key> <string>iPhone 6S Plus</string> <key>iPhone8,4</key> <string>iPhone SE</string> <key>iPhone9,1</key> <string>iPhone 7</string> <key>iPhone9,2</key> <string>iPhone 7 Plus</string> <key>iPhone9,3</key> <string>iPhone 7</string> <key>iPhone9,4</key> <string>iPhone 7 Plus</string> <key>iPhone10,1</key> <string>iPhone 8</string> <key>iPhone10,4</key> <string>iPhone 8</string> <key>iPhone10,2</key> <string>iPhone 8 Plus</string> <key>iPhone10,5</key> <string>iPhone 8 Plus</string> <key>iPhone10,3</key> <string>iPhone X</string> <key>iPhone10,6</key> <string>iPhone X</string> <key>iPhone11,2</key> <string>iPhone XS</string> <key>iPhone11,4</key> <string>iPhone XS Max</string> <key>iPhone11,6</key> <string>iPhone XS Max</string> <key>iPhone11,8</key> <string>iPhone XR</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>iPad4,3</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> <key>iPad4,6</key> <string>iPad Mini 2nd Gen</string> <key>iPad4,7</key> <string>iPad Mini 3rd Gen</string> <key>iPad4,8</key> <string>iPad Mini 3rd Gen</string> <key>iPad4,9</key> <string>iPad Mini 3rd Gen</string> <key>iPad5,1</key> <string>iPad Mini 4</string> <key>iPad5,2</key> <string>iPad Mini 4</string> <key>iPad5,3</key> <string>iPad Air 2</string> <key>iPad5,4</key> <string>iPad Air 2</string> <key>iPad6,3</key> <string>iPad Pro 9.7 inch</string> <key>iPad6,4</key> <string>iPad Pro 9.7 inch</string> <key>iPad6,7</key> <string>iPad Pro 12.9 inch</string> <key>iPad6,8</key> <string>iPad Pro 12.9 inch</string> <key>iPad7,1</key> <string>iPad Pro 12.9 inch 2nd Gen</string> <key>iPad7,2</key> <string>iPad Pro 12.9 inch 2nd Gen</string> <key>iPad7,3</key> <string>iPad Pro 10.5 inch</string> <key>iPad7,4</key> <string>iPad Pro 10.5 inch</string> <key>iPad8,1</key> <string>iPad Pro 11 inch</string> <key>iPad8,2</key> <string>iPad Pro 11 inch</string> <key>iPad8,3</key> <string>iPad Pro 11 inch</string> <key>iPad8,4</key> <string>iPad Pro 11 inch</string> <key>iPad8,5</key> <string>iPad Pro 12.9 inch 3rd Gen</string> <key>iPad8,6</key> <string>iPad Pro 12.9 inch 3rd Gen</string> <key>iPad8,7</key> <string>iPad Pro 12.9 inch 3rd Gen</string> <key>iPad8,8</key> <string>iPad Pro 12.9 inch 3rd Gen</string> </dict> </plist> |
编码此方法:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <sys/sysctl.h> ... + (NSString *)getModel { size_t size; sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *model = malloc(size); sysctlbyname("hw.machine", model, &size, NULL, 0); NSString *deviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding]; free(model); return deviceModel; } |
当你需要模型的时候调用方法
一个好的实践是创建一个名为
下面是代码(代码可能不包含所有设备的字符串,我和其他人在GitHub上维护相同的代码,所以请从中获取最新的代码)
目标C:Github/DeviceUtil
银行代码:github/deviceguur
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <sys/types.h> #include <sys/sysctl.h> - (NSString*)hardwareDescription { NSString *hardware = [self hardwareString]; if ([hardware isEqualToString:@"iPhone1,1"]) return @"iPhone 2G"; if ([hardware isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([hardware isEqualToString:@"iPhone3,1"]) return @"iPhone 4"; if ([hardware isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; if ([hardware isEqualToString:@"iPhone5,1"]) return @"iPhone 5"; if ([hardware isEqualToString:@"iPod1,1"]) return @"iPodTouch 1G"; if ([hardware isEqualToString:@"iPod2,1"]) return @"iPodTouch 2G"; if ([hardware isEqualToString:@"iPad1,1"]) return @"iPad"; if ([hardware isEqualToString:@"iPad2,6"]) return @"iPad Mini"; if ([hardware isEqualToString:@"iPad4,1"]) return @"iPad Air WIFI"; //there are lots of other strings too, checkout the github repo //link is given at the top of this answer if ([hardware isEqualToString:@"i386"]) return @"Simulator"; if ([hardware isEqualToString:@"x86_64"]) return @"Simulator"; return nil; } - (NSString*)hardwareString { size_t size = 100; char *hw_machine = malloc(size); int name[] = {CTL_HW,HW_MACHINE}; sysctl(name, 2, hw_machine, &size, NULL, 0); NSString *hardware = [NSString stringWithUTF8String:hw_machine]; free(hw_machine); return hardware; } |
我已经优化了Nicolasmiari的实现,并添加了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | + (NSString*)deviceName { static NSDictionary* deviceNamesByCode = nil; static NSString* deviceName = nil; if (deviceName) { return deviceName; } 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) }; struct utsname systemInfo; uname(&systemInfo); NSString* code = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; deviceName = [deviceNamesByCode objectForKey:code]; if (!deviceName) { // Not found in database. At least guess main device type from string contents: if ([code rangeOfString:@"iPod"].location != NSNotFound) { deviceName = @"iPod Touch"; } else if([code rangeOfString:@"iPad"].location != NSNotFound) { deviceName = @"iPad"; } else if([code rangeOfString:@"iPhone"].location != NSNotFound){ deviceName = @"iPhone"; } else { deviceName = @"Simulator"; } } return deviceName; } |
` `
脱离nsstring描述的类别
一般来说,最好避免在代码中进行任意字符串比较。最好在一个地方更新字符串,并从应用程序中隐藏魔法字符串。为此,我提供了一个关于
对于我的特定需求,我需要知道我使用的是哪个设备,而不需要知道关于网络功能的具体信息,这些信息可以通过其他方式轻松检索到。因此,您将发现一个比不断增长的设备列表更粗粒度的
更新就是将设备添加到枚举和查找表中。
uidevice+ntnuextensions.h(设备+ntnuextensions.h)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | typedef NS_ENUM(NSUInteger, NTNUDeviceType) { DeviceAppleUnknown, DeviceAppleSimulator, DeviceAppleiPhone, DeviceAppleiPhone3G, DeviceAppleiPhone3GS, DeviceAppleiPhone4, DeviceAppleiPhone4S, DeviceAppleiPhone5, DeviceAppleiPhone5C, DeviceAppleiPhone5S, DeviceAppleiPhone6, DeviceAppleiPhone6_Plus, DeviceAppleiPhone6S, DeviceAppleiPhone6S_Plus, DeviceAppleiPhoneSE, DeviceAppleiPhone7, DeviceAppleiPhone7_Plus, DeviceAppleiPodTouch, DeviceAppleiPodTouch2G, DeviceAppleiPodTouch3G, DeviceAppleiPodTouch4G, DeviceAppleiPad, DeviceAppleiPad2, DeviceAppleiPad3G, DeviceAppleiPad4G, DeviceAppleiPad5G_Air, DeviceAppleiPadMini, DeviceAppleiPadMini2G, DeviceAppleiPadPro12, DeviceAppleiPadPro9 }; @interface UIDevice (NTNUExtensions) - (NSString *)ntnu_deviceDescription; - (NTNUDeviceType)ntnu_deviceType; @end |
uidevice+ntnuextensions.m版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #import <sys/utsname.h> #import"UIDevice+NTNUExtensions.h" @implementation UIDevice (NTNUExtensions) - (NSString *)ntnu_deviceDescription { struct utsname systemInfo; uname(&systemInfo); return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; } - (NTNUDeviceType)ntnu_deviceType { NSNumber *deviceType = [[self ntnu_deviceTypeLookupTable] objectForKey:[self ntnu_deviceDescription]]; return [deviceType unsignedIntegerValue]; } - (NSDictionary *)ntnu_deviceTypeLookupTable { return @{ @"i386": @(DeviceAppleSimulator), @"x86_64": @(DeviceAppleSimulator), @"iPod1,1": @(DeviceAppleiPodTouch), @"iPod2,1": @(DeviceAppleiPodTouch2G), @"iPod3,1": @(DeviceAppleiPodTouch3G), @"iPod4,1": @(DeviceAppleiPodTouch4G), @"iPhone1,1": @(DeviceAppleiPhone), @"iPhone1,2": @(DeviceAppleiPhone3G), @"iPhone2,1": @(DeviceAppleiPhone3GS), @"iPhone3,1": @(DeviceAppleiPhone4), @"iPhone3,3": @(DeviceAppleiPhone4), @"iPhone4,1": @(DeviceAppleiPhone4S), @"iPhone5,1": @(DeviceAppleiPhone5), @"iPhone5,2": @(DeviceAppleiPhone5), @"iPhone5,3": @(DeviceAppleiPhone5C), @"iPhone5,4": @(DeviceAppleiPhone5C), @"iPhone6,1": @(DeviceAppleiPhone5S), @"iPhone6,2": @(DeviceAppleiPhone5S), @"iPhone7,1": @(DeviceAppleiPhone6_Plus), @"iPhone7,2": @(DeviceAppleiPhone6), @"iPhone8,1" :@(DeviceAppleiPhone6S), @"iPhone8,2" :@(DeviceAppleiPhone6S_Plus), @"iPhone8,4" :@(DeviceAppleiPhoneSE), @"iPhone9,1" :@(DeviceAppleiPhone7), @"iPhone9,3" :@(DeviceAppleiPhone7), @"iPhone9,2" :@(DeviceAppleiPhone7_Plus), @"iPhone9,4" :@(DeviceAppleiPhone7_Plus), @"iPad1,1": @(DeviceAppleiPad), @"iPad2,1": @(DeviceAppleiPad2), @"iPad3,1": @(DeviceAppleiPad3G), @"iPad3,4": @(DeviceAppleiPad4G), @"iPad2,5": @(DeviceAppleiPadMini), @"iPad4,1": @(DeviceAppleiPad5G_Air), @"iPad4,2": @(DeviceAppleiPad5G_Air), @"iPad4,4": @(DeviceAppleiPadMini2G), @"iPad4,5": @(DeviceAppleiPadMini2G), @"iPad4,7":@(DeviceAppleiPadMini), @"iPad6,7":@(DeviceAppleiPadPro12), @"iPad6,8":@(DeviceAppleiPadPro12), @"iPad6,3":@(DeviceAppleiPadPro9), @"iPad6,4":@(DeviceAppleiPadPro9) }; } @end |
设备名和机器名,基于iOS真实设备硬件
我对这个问题的解决方案基本上与这个线程中的@nicolasmiari answer相关。单独的实用程序类有一组预定义的设备和机器名,然后根据实际的机器名检索设备名。
注:截至2017年10月,此答案及其链接的Github项目已更新,以识别当前最新的iPhone(iPhone 8、8+、X)。这也适用于IOS11。请访问Github回购协议并查看,如果有问题请给我反馈。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | /* * Retrieves back the device name or if not the machine name. */ + (NSString*)deviceModelName { struct utsname systemInfo; uname(&systemInfo); NSString *machineName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; //MARK: More official list is at //http://theiphonewiki.com/wiki/Models //MARK: You may just return machineName. Following is for convenience NSDictionary *commonNamesDictionary = @{ @"i386": @"i386 Simulator", @"x86_64": @"x86_64 Simulator", @"iPhone1,1": @"iPhone", @"iPhone1,2": @"iPhone 3G", @"iPhone2,1": @"iPhone 3GS", @"iPhone3,1": @"iPhone 4", @"iPhone3,2": @"iPhone 4(Rev A)", @"iPhone3,3": @"iPhone 4(CDMA)", @"iPhone4,1": @"iPhone 4S", @"iPhone5,1": @"iPhone 5(GSM)", @"iPhone5,2": @"iPhone 5(GSM+CDMA)", @"iPhone5,3": @"iPhone 5c(GSM)", @"iPhone5,4": @"iPhone 5c(GSM+CDMA)", @"iPhone6,1": @"iPhone 5s(GSM)", @"iPhone6,2": @"iPhone 5s(GSM+CDMA)", @"iPhone7,1": @"iPhone 6+(GSM+CDMA)", @"iPhone7,2": @"iPhone 6(GSM+CDMA)", @"iPhone8,1": @"iPhone 6S(GSM+CDMA)", @"iPhone8,2": @"iPhone 6S+(GSM+CDMA)", @"iPhone8,4": @"iPhone SE(GSM+CDMA)", @"iPhone9,1": @"iPhone 7(GSM+CDMA)", @"iPhone9,2": @"iPhone 7+(GSM+CDMA)", @"iPhone9,3": @"iPhone 7(GSM+CDMA)", @"iPhone9,4": @"iPhone 7+(GSM+CDMA)", @"iPad1,1": @"iPad", @"iPad2,1": @"iPad 2(WiFi)", @"iPad2,2": @"iPad 2(GSM)", @"iPad2,3": @"iPad 2(CDMA)", @"iPad2,4": @"iPad 2(WiFi Rev A)", @"iPad2,5": @"iPad Mini 1G (WiFi)", @"iPad2,6": @"iPad Mini 1G (GSM)", @"iPad2,7": @"iPad Mini 1G (GSM+CDMA)", @"iPad3,1": @"iPad 3(WiFi)", @"iPad3,2": @"iPad 3(GSM+CDMA)", @"iPad3,3": @"iPad 3(GSM)", @"iPad3,4": @"iPad 4(WiFi)", @"iPad3,5": @"iPad 4(GSM)", @"iPad3,6": @"iPad 4(GSM+CDMA)", @"iPad4,1": @"iPad Air(WiFi)", @"iPad4,2": @"iPad Air(GSM)", @"iPad4,3": @"iPad Air(GSM+CDMA)", @"iPad5,3": @"iPad Air 2 (WiFi)", @"iPad5,4": @"iPad Air 2 (GSM+CDMA)", @"iPad4,4": @"iPad Mini 2G (WiFi)", @"iPad4,5": @"iPad Mini 2G (GSM)", @"iPad4,6": @"iPad Mini 2G (GSM+CDMA)", @"iPad4,7": @"iPad Mini 3G (WiFi)", @"iPad4,8": @"iPad Mini 3G (GSM)", @"iPad4,9": @"iPad Mini 3G (GSM+CDMA)", @"iPod1,1": @"iPod 1st Gen", @"iPod2,1": @"iPod 2nd Gen", @"iPod3,1": @"iPod 3rd Gen", @"iPod4,1": @"iPod 4th Gen", @"iPod5,1": @"iPod 5th Gen", @"iPod7,1": @"iPod 6th Gen", }; NSString *deviceName = commonNamesDictionary[machineName]; if (deviceName == nil) { deviceName = machineName; } return deviceName; } |
我在一个类中添加了这个实现和其他一些方便的实用工具方法,并将其放到这个Github存储库中。您还可以在这个wiki页面中找到最新的设备信息列表。
请参观并使用它。
更新:
您可能需要导入sys framework,
1 | #import <sys/utsname.h> |
兼容Swift 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | // MARK: - UIDevice Extension - private let DeviceList = [ /* iPod 5 */ "iPod5,1":"iPod Touch 5", /* iPhone 4 */ "iPhone3,1": "iPhone 4","iPhone3,2":"iPhone 4","iPhone3,3":"iPhone 4", /* iPhone 4S */ "iPhone4,1":"iPhone 4S", /* iPhone 5 */ "iPhone5,1":"iPhone 5","iPhone5,2":"iPhone 5", /* iPhone 5C */ "iPhone5,3":"iPhone 5C","iPhone5,4":"iPhone 5C", /* iPhone 5S */ "iPhone6,1":"iPhone 5S","iPhone6,2":"iPhone 5S", /* iPhone 6 */ "iPhone7,2":"iPhone 6", /* iPhone 6 Plus */ "iPhone7,1":"iPhone 6 Plus", /* iPhone 6S */ "iPhone8,1":"iPhone 6S", /* iPhone 6S Plus */ "iPhone8,2":"iPhone 6S Plus", /* iPhone SE */ "iPhone8,4":"iPhone SE", /* iPhone 7 */ "iPhone9,1":"iPhone 7", /* iPhone 7 */ "iPhone9,3":"iPhone 7", /* iPhone 7 Plus */ "iPhone9,2":"iPhone 7 Plus", /* iPhone 7 Plus */ "iPhone9,4":"iPhone 7 Plus", /* iPad 2 */ "iPad2,1":"iPad 2","iPad2,2":"iPad 2","iPad2,3":"iPad 2","iPad2,4":"iPad 2", /* iPad 3 */ "iPad3,1":"iPad 3","iPad3,2":"iPad 3","iPad3,3": "iPad 3", /* iPad 4 */ "iPad3,4":"iPad 4","iPad3,5":"iPad 4","iPad3,6":"iPad 4", /* iPad Air */ "iPad4,1":"iPad Air","iPad4,2":"iPad Air","iPad4,3":"iPad Air", /* iPad Air 2 */ "iPad5,1":"iPad Air 2","iPad5,3":"iPad Air 2","iPad5,4":"iPad Air 2", /* iPad Mini */ "iPad2,5":"iPad Mini 1","iPad2,6":"iPad Mini 1","iPad2,7":"iPad Mini 1", /* iPad Mini 2 */ "iPad4,4":"iPad Mini 2","iPad4,5":"iPad Mini 2","iPad4,6":"iPad Mini 2", /* iPad Mini 3 */ "iPad4,7":"iPad Mini 3","iPad4,8":"iPad Mini 3","iPad4,9":"iPad Mini 3", /* iPad Pro 12.9 */ "iPad6,7":"iPad Pro 12.9","iPad6,8":"iPad Pro 12.9", /* iPad Pro 9.7 */ "iPad6,3":"iPad Pro 9.7","iPad6,4":"iPad Pro 9.7", /* Simulator */ "x86_64":"Simulator","i386":"Simulator" ] extension UIDevice { static var modelName: String { var systemInfo = utsname() uname(&systemInfo) let machine = systemInfo.machine let mirror = Mirror(reflecting: machine) var identifier ="" for child in mirror.children { if let value = child.value as? Int8, value != 0 { identifier += String(UnicodeScalar(UInt8(value))) } } return DeviceList[identifier] ?? identifier } static var isIphone4: Bool { return modelName =="iPhone 5" || modelName =="iPhone 5C" || modelName =="iPhone 5S" || UIDevice.isSimulatorIPhone4 } static var isIphone5: Bool { return modelName =="iPhone 4S" || modelName =="iPhone 4" || UIDevice.isSimulatorIPhone5 } static var isIphone6: Bool { return modelName =="iPhone 6" || UIDevice.isSimulatorIPhone6 } static var isIphone6Plus: Bool { return modelName =="iPhone 6 Plus" || UIDevice.isSimulatorIPhone6Plus } static var isIpad: Bool { if UIDevice.current.model.contains("iPad") { return true } return false } static var isIphone: Bool { return !self.isIpad } /// Check if current device is iPhone4S (and earlier) relying on screen heigth static var isSimulatorIPhone4: Bool { return UIDevice.isSimulatorWithScreenHeigth(480) } /// Check if current device is iPhone5 relying on screen heigth static var isSimulatorIPhone5: Bool { return UIDevice.isSimulatorWithScreenHeigth(568) } /// Check if current device is iPhone6 relying on screen heigth static var isSimulatorIPhone6: Bool { return UIDevice.isSimulatorWithScreenHeigth(667) } /// Check if current device is iPhone6 Plus relying on screen heigth static var isSimulatorIPhone6Plus: Bool { return UIDevice.isSimulatorWithScreenHeigth(736) } private static func isSimulatorWithScreenHeigth(_ heigth: CGFloat) -> Bool { let screenSize: CGRect = UIScreen.main.bounds return modelName =="Simulator" && screenSize.height == heigth } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #import <sys/utsname.h> #define HARDWARE @{@"i386": @"Simulator",@"x86_64": @"Simulator",@"iPod1,1": @"iPod Touch",@"iPod2,1": @"iPod Touch 2nd Generation",@"iPod3,1": @"iPod Touch 3rd Generation",@"iPod4,1": @"iPod Touch 4th Generation",@"iPhone1,1": @"iPhone",@"iPhone1,2": @"iPhone 3G",@"iPhone2,1": @"iPhone 3GS",@"iPhone3,1": @"iPhone 4",@"iPhone4,1": @"iPhone 4S",@"iPhone5,1": @"iPhone 5",@"iPhone5,2": @"iPhone 5",@"iPhone5,3": @"iPhone 5c",@"iPhone5,4": @"iPhone 5c",@"iPhone6,1": @"iPhone 5s",@"iPhone6,2": @"iPhone 5s",@"iPad1,1": @"iPad",@"iPad2,1": @"iPad 2",@"iPad3,1": @"iPad 3rd Generation",@"iPad3,4": @"iPad 4th Generation",@"iPad2,5": @"iPad Mini",@"iPad4,4": @"iPad Mini 2nd Generation - Wifi",@"iPad4,5": @"iPad Mini 2nd Generation - Cellular",@"iPad4,1": @"iPad Air 5th Generation - Wifi",@"iPad4,2": @"iPad Air 5th Generation - Cellular"} @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; struct utsname systemInfo; uname(&systemInfo); NSLog(@"hardware: %@",[HARDWARE objectForKey:[NSString stringWithCString: systemInfo.machine encoding:NSUTF8StringEncoding]]); } |
此解决方案既适用于物理设备,也适用于iOS模拟器,因为模拟器返回的模型与物理设备返回的模型相同,例如iPhoneX(GSM)的"iphone10,6",而不是"x86_64"。
定义-Swift 4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import UIKit extension UIDevice { var modelName: String { var systemInfo = utsname() uname(&systemInfo) let machineMirror = Mirror(reflecting: systemInfo.machine) let identifier = machineMirror.children.reduce("") { identifier, element in guard let value = element.value as? Int8, value != 0 else { return identifier } return identifier + String(UnicodeScalar(UInt8(value))) } if let value = ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"] { return value } else { return identifier } } } |
用途:
1 | print(UIDevice.current.modelName) |
以下功能可在iOS7或更高版本的swift中完美工作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | func DeviceName()-> String { var myDeviceName : String = String() var systemInfo = [UInt8](count: sizeof(utsname), repeatedValue: 0) let model = systemInfo.withUnsafeMutableBufferPointer { (inout body: UnsafeMutableBufferPointer<UInt8>) -> String? in if uname(UnsafeMutablePointer(body.baseAddress)) != 0 { return nil } return String.fromCString(UnsafePointer(body.baseAddress.advancedBy(Int(_SYS_NAMELEN * 4)))) } let deviceNamesByCode : [String: String] = ["iPod1,1":"iPod Touch 1", "iPod2,1":"iPod Touch 2", "iPod3,1":"iPod Touch 3", "iPod4,1":"iPod Touch 4", "iPod5,1":"iPod Touch 5", "iPod7,1":"iPod Touch 6", "iPhone1,1":"iPhone", "iPhone1,2":"iPhone", "iPhone2,1":"iPhone", "iPhone3,1":"iPhone 4", "iPhone3,2":"iPhone 4", "iPhone3,3":"iPhone 4", "iPhone4,1":"iPhone 4s", "iPhone5,1":"iPhone 5", "iPhone5,2":"iPhone 5", "iPhone5,3":"iPhone 5c", "iPhone5,4":"iPhone 5c", "iPhone6,1":"iPhone 5s", "iPhone6,2":"iPhone 5s", "iPhone7,2":"iPhone 6", "iPhone7,1":"iPhone 6 Plus", "iPhone8,1":"iPhone 6s", "iPhone8,2":"iPhone 6s Plus", "iPhone8,4":"iPhone SE", "iPad2,1":"iPad 2", "iPad2,2":"iPad 2", "iPad2,3":"iPad 2", "iPad2,4":"iPad 2", "iPad3,1":"iPad 3", "iPad3,2":"iPad 3", "iPad3,3":"iPad 3", "iPad3,4":"iPad 4", "iPad3,5":"iPad 4", "iPad3,6":"iPad 4", "iPad4,1":"iPad Air", "iPad4,2":"iPad Air", "iPad4,3":"iPad Air", "iPad5,3":"iPad Air 2", "iPad5,4":"iPad Air 2", "iPad2,5":"iPad Mini", "iPad2,6":"iPad Mini", "iPad2,7":"iPad Mini", "iPad4,4":"iPad Mini 2", "iPad4,5":"iPad Mini 2", "iPad4,6":"iPad Mini 2", "iPad4,7":"iPad Mini 3", "iPad4,8":"iPad Mini 3", "iPad4,9":"iPad Mini 3", "iPad5,1":"iPad Mini 4", "iPad5,2":"iPad Mini 4", "iPad6,3":"iPad Pro", "iPad6,4":"iPad Pro", "iPad6,7":"iPad Pro", "iPad6,8":"iPad Pro", "AppleTV5,3":"Apple TV", "i386":"Simulator", "x86_64":"Simulator" ] if model!.characters.count > 0 { myDeviceName = deviceNamesByCode[model!]! }else{ myDeviceName = UIDevice.currentDevice().model } print("myDeviceName==\(myDeviceName)") return myDeviceName } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | // // UIDevice+Hardware.h // gauravds // #import <UIKit/UIKit.h> @interface UIDevice (Hardware) - (NSString *)modelIdentifier; - (NSString *)modelName; @end |
为什么不创建一个类别呢?不要使用malloc方法安全泄漏问题。使用安全功能。
1 | NSLog(@"%@ %@", [[UIDevice currentDevice] modelName], [[UIDevice currentDevice] modelIdentifier]); |
见实施例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | // // UIDevice+Hardware.m // gauravds // #import"UIDevice+Hardware.h" #import <sys/utsname.h> @implementation UIDevice (Hardware) - (NSString *)modelIdentifier { struct utsname systemInfo; uname(&systemInfo); return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; } - (NSString *)modelName { return [self modelNameForModelIdentifier:[self modelIdentifier]]; } - (NSString *)modelNameForModelIdentifier:(NSString *)modelIdentifier { // iPhone http://theiphonewiki.com/wiki/IPhone if ([modelIdentifier isEqualToString:@"iPhone1,1"]) return @"iPhone 1G"; if ([modelIdentifier isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([modelIdentifier isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; if ([modelIdentifier isEqualToString:@"iPhone3,1"]) return @"iPhone 4 (GSM)"; if ([modelIdentifier isEqualToString:@"iPhone3,2"]) return @"iPhone 4 (GSM Rev A)"; if ([modelIdentifier isEqualToString:@"iPhone3,3"]) return @"iPhone 4 (CDMA)"; if ([modelIdentifier isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; if ([modelIdentifier isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)"; if ([modelIdentifier isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (Global)"; if ([modelIdentifier isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (GSM)"; if ([modelIdentifier isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (Global)"; if ([modelIdentifier isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (GSM)"; if ([modelIdentifier isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (Global)"; if ([modelIdentifier isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus"; if ([modelIdentifier isEqualToString:@"iPhone7,2"]) return @"iPhone 6"; if ([modelIdentifier isEqualToString:@"iPhone8,1"]) return @"iPhone 6s"; if ([modelIdentifier isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus"; if ([modelIdentifier isEqualToString:@"iPhone8,4"]) return @"iPhone SE"; if ([modelIdentifier isEqualToString:@"iPhone9,1"]) return @"iPhone 7"; if ([modelIdentifier isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus"; if ([modelIdentifier isEqualToString:@"iPhone9,3"]) return @"iPhone 7"; if ([modelIdentifier isEqualToString:@"iPhone9,4"]) return @"iPhone 7 Plus"; if ([modelIdentifier isEqualToString:@"iPhone10,1"]) return @"iPhone 8"; // US (Verizon), China, Japan if ([modelIdentifier isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus"; // US (Verizon), China, Japan if ([modelIdentifier isEqualToString:@"iPhone10,3"]) return @"iPhone X"; // US (Verizon), China, Japan if ([modelIdentifier isEqualToString:@"iPhone10,4"]) return @"iPhone 8"; // AT&T, Global if ([modelIdentifier isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus"; // AT&T, Global if ([modelIdentifier isEqualToString:@"iPhone10,6"]) return @"iPhone X"; // AT&T, Global // iPad http://theiphonewiki.com/wiki/IPad if ([modelIdentifier isEqualToString:@"iPad1,1"]) return @"iPad 1G"; if ([modelIdentifier isEqualToString:@"iPad2,1"]) return @"iPad 2 (Wi-Fi)"; if ([modelIdentifier isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)"; if ([modelIdentifier isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)"; if ([modelIdentifier isEqualToString:@"iPad2,4"]) return @"iPad 2 (Rev A)"; if ([modelIdentifier isEqualToString:@"iPad3,1"]) return @"iPad 3 (Wi-Fi)"; if ([modelIdentifier isEqualToString:@"iPad3,2"]) return @"iPad 3 (GSM)"; if ([modelIdentifier isEqualToString:@"iPad3,3"]) return @"iPad 3 (Global)"; if ([modelIdentifier isEqualToString:@"iPad3,4"]) return @"iPad 4 (Wi-Fi)"; if ([modelIdentifier isEqualToString:@"iPad3,5"]) return @"iPad 4 (GSM)"; if ([modelIdentifier isEqualToString:@"iPad3,6"]) return @"iPad 4 (Global)"; if ([modelIdentifier isEqualToString:@"iPad6,11"]) return @"iPad (5th gen) (Wi-Fi)"; if ([modelIdentifier isEqualToString:@"iPad6,12"]) return @"iPad (5th gen) (Cellular)"; if ([modelIdentifier isEqualToString:@"iPad4,1"]) return @"iPad Air (Wi-Fi)"; if ([modelIdentifier isEqualToString:@"iPad4,2"]) return @"iPad Air (Cellular)"; if ([modelIdentifier isEqualToString:@"iPad5,3"]) return @"iPad Air 2 (Wi-Fi)"; if ([modelIdentifier isEqualToString:@"iPad5,4"]) return @"iPad Air 2 (Cellular)"; // iPad Mini http://theiphonewiki.com/wiki/IPad_mini if ([modelIdentifier isEqualToString:@"iPad2,5"]) return @"iPad mini 1G (Wi-Fi)"; if ([modelIdentifier isEqualToString:@"iPad2,6"]) return @"iPad mini 1G (GSM)"; if ([modelIdentifier isEqualToString:@"iPad2,7"]) return @"iPad mini 1G (Global)"; if ([modelIdentifier isEqualToString:@"iPad4,4"]) return @"iPad mini 2G (Wi-Fi)"; if ([modelIdentifier isEqualToString:@"iPad4,5"]) return @"iPad mini 2G (Cellular)"; if ([modelIdentifier isEqualToString:@"iPad4,6"]) return @"iPad mini 2G (Cellular)"; // TD-LTE model see https://support.apple.com/en-us/HT201471#iPad-mini2 if ([modelIdentifier isEqualToString:@"iPad4,7"]) return @"iPad mini 3G (Wi-Fi)"; if ([modelIdentifier isEqualToString:@"iPad4,8"]) return @"iPad mini 3G (Cellular)"; if ([modelIdentifier isEqualToString:@"iPad4,9"]) return @"iPad mini 3G (Cellular)"; if ([modelIdentifier isEqualToString:@"iPad5,1"]) return @"iPad mini 4G (Wi-Fi)"; if ([modelIdentifier isEqualToString:@"iPad5,2"]) return @"iPad mini 4G (Cellular)"; // iPad Pro https://www.theiphonewiki.com/wiki/IPad_Pro if ([modelIdentifier isEqualToString:@"iPad6,3"]) return @"iPad Pro (9.7 inch) 1G (Wi-Fi)"; // http://pdadb.net/index.php?m=specs&id=9938&c=apple_ipad_pro_9.7-inch_a1673_wifi_32gb_apple_ipad_6,3 if ([modelIdentifier isEqualToString:@"iPad6,4"]) return @"iPad Pro (9.7 inch) 1G (Cellular)"; // http://pdadb.net/index.php?m=specs&id=9981&c=apple_ipad_pro_9.7-inch_a1675_td-lte_32gb_apple_ipad_6,4 if ([modelIdentifier isEqualToString:@"iPad6,7"]) return @"iPad Pro (12.9 inch) 1G (Wi-Fi)"; // http://pdadb.net/index.php?m=specs&id=8960&c=apple_ipad_pro_wifi_a1584_128gb if ([modelIdentifier isEqualToString:@"iPad6,8"]) return @"iPad Pro (12.9 inch) 1G (Cellular)"; // http://pdadb.net/index.php?m=specs&id=8965&c=apple_ipad_pro_td-lte_a1652_32gb_apple_ipad_6,8 if ([modelIdentifier isEqualToString:@"iPad 7,1"]) return @"iPad Pro (12.9 inch) 2G (Wi-Fi)"; if ([modelIdentifier isEqualToString:@"iPad 7,2"]) return @"iPad Pro (12.9 inch) 2G (Cellular)"; if ([modelIdentifier isEqualToString:@"iPad 7,3"]) return @"iPad Pro (10.5 inch) 1G (Wi-Fi)"; if ([modelIdentifier isEqualToString:@"iPad 7,4"]) return @"iPad Pro (10.5 inch) 1G (Cellular)"; // iPod http://theiphonewiki.com/wiki/IPod if ([modelIdentifier isEqualToString:@"iPod1,1"]) return @"iPod touch 1G"; if ([modelIdentifier isEqualToString:@"iPod2,1"]) return @"iPod touch 2G"; if ([modelIdentifier isEqualToString:@"iPod3,1"]) return @"iPod touch 3G"; if ([modelIdentifier isEqualToString:@"iPod4,1"]) return @"iPod touch 4G"; if ([modelIdentifier isEqualToString:@"iPod5,1"]) return @"iPod touch 5G"; if ([modelIdentifier isEqualToString:@"iPod7,1"]) return @"iPod touch 6G"; // as 6,1 was never released 7,1 is actually 6th generation // Apple TV https://www.theiphonewiki.com/wiki/Apple_TV if ([modelIdentifier isEqualToString:@"AppleTV1,1"]) return @"Apple TV 1G"; if ([modelIdentifier isEqualToString:@"AppleTV2,1"]) return @"Apple TV 2G"; if ([modelIdentifier isEqualToString:@"AppleTV3,1"]) return @"Apple TV 3G"; if ([modelIdentifier isEqualToString:@"AppleTV3,2"]) return @"Apple TV 3G"; // small, incremental update over 3,1 if ([modelIdentifier isEqualToString:@"AppleTV5,3"]) return @"Apple TV 4G"; // as 4,1 was never released, 5,1 is actually 4th generation // Simulator if ([modelIdentifier hasSuffix:@"86"] || [modelIdentifier isEqual:@"x86_64"]) { BOOL smallerScreen = ([[UIScreen mainScreen] bounds].size.width < 768.0); return (smallerScreen ? @"iPhone Simulator" : @"iPad Simulator"); } return modelIdentifier; } @end |
Swift 4或更高版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | extension UIDevice { var modelName: String { if let modelName = ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"] { return modelName } var info = utsname() uname(&info) return String(String.UnicodeScalarView( Mirror(reflecting: info.machine) .children .compactMap { guard let value = $0.value as? Int8 else { return nil } let unicode = UnicodeScalar(UInt8(value)) return unicode.isASCII ? unicode : nil })) } } |
1 | UIDevice.current.modelName //"iPad6,4" |
使用ideviceinfo获取这些值怎么样?应能使用
然后运行:
1 2 3 | PRODUCT_NAME=$(ideviceinfo --udid $DEVICE_UDID --key ProductName) PRODUCT_TYPE=$(ideviceinfo --udid $DEVICE_UDID --key ProductType) PRODUCT_VERSION=$(ideviceinfo --udid $DEVICE_UDID --key ProductVersion) |
高温高压
-杰姆斯
如果您有一系列设备(如https://stackoverflow.com/a/17655825/849616中由@tib维护)来处理它,如果您使用swift 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | extension UIDevice { /// Fetches the information about the name of the device. /// /// - Returns: Should return meaningful device name, if not found will return device system code. public static func modelName() -> String { let physicalName = deviceSystemCode() if let deviceTypes = deviceTypes(), let modelName = deviceTypes[physicalName] as? String { return modelName } return physicalName } } private extension UIDevice { /// Fetches from system the code of the device static func deviceSystemCode() -> String { var systemInfo = utsname() uname(&systemInfo) let machineMirror = Mirror(reflecting: systemInfo.machine) let identifier = machineMirror.children.reduce("") { identifier, element in guard let value = element.value as? Int8, value != 0 else { return identifier } return identifier + String(UnicodeScalar(UInt8(value))) } return identifier } /// Fetches the plist entries from plist maintained in https://stackoverflow.com/a/17655825/849616 /// /// - Returns: A dictionary with pairs of deviceSystemCode <-> meaningfulDeviceName. static func deviceTypes() -> NSDictionary? { if let fileUrl = Bundle.main.url(forResource:"your plist name", withExtension:"plist"), let configurationDictionary = NSDictionary(contentsOf: fileUrl) { return configurationDictionary } return nil } } |
稍后您可以使用
其他学分转到@tib(用于plist),@aniruddh joshi(用于
为了节省别人的时间。因为接受了,必须投票回答使用私有API的建议库,而我的应用程序因为昨天的uidevice扩展而被拒绝!我正在切换到gbdeviceinfo库,同时也安装了pod,目前它看起来不错,维护和更新到最新的设备。