Determine device (iPhone, iPod Touch) with iOS
是否有方法确定运行应用程序的设备。如果可能的话,我想区分
您可以这样使用
1 2 3 4 | NSString *deviceType = [UIDevice currentDevice].model; if([deviceType isEqualToString:@"iPhone"]) // it's an iPhone |
这是上述答案对
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 | - (NSString *)platformString { NSString *platform = [self platform]; if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G"; if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4"; if ([platform isEqualToString:@"iPhone3,3"]) return @"Verizon iPhone 4"; if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)"; if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (GSM)"; if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (GSM)"; if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6"; if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6 Plus"; if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s"; if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus"; if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE"; if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G"; if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G"; if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G"; if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G"; if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G"; if ([platform isEqualToString:@"iPod7,1"]) return @"iPod Touch 6G"; if ([platform isEqualToString:@"iPad1,1"]) return @"iPad"; if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)"; if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)"; if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)"; if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2 (WiFi)"; if ([platform isEqualToString:@"iPad2,5"]) return @"iPad Mini (WiFi)"; if ([platform isEqualToString:@"iPad2,6"]) return @"iPad Mini (GSM)"; if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini (GSM+CDMA)"; if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)"; if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (GSM+CDMA)"; if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3 (GSM)"; if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)"; if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4 (GSM)"; if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4 (GSM+CDMA)"; if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air (WiFi)"; if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air (Cellular)"; if ([platform isEqualToString:@"iPad4,4"]) return @"iPad mini 2G (WiFi)"; if ([platform isEqualToString:@"iPad4,5"]) return @"iPad mini 2G (Cellular)"; if ([platform isEqualToString:@"iPad5,1"]) return @"iPad mini 4 (WiFi)"; if ([platform isEqualToString:@"iPad5,2"]) return @"iPad mini 4 (Cellular)"; if ([platform isEqualToString:@"iPad5,3"]) return @"iPad Air 2 (WiFi)"; if ([platform isEqualToString:@"iPad5,4"]) return @"iPad Air 2 (Cellular)"; if ([platform isEqualToString:@"iPad6,3"]) return @"iPad Pro 9.7 inch (WiFi)"; if ([platform isEqualToString:@"iPad6,4"]) return @"iPad Pro 9.7 inch (Cellular)"; if ([platform isEqualToString:@"iPad6,7"]) return @"iPad Pro 12.9 inch (WiFi)"; if ([platform isEqualToString:@"iPad6,8"]) return @"iPad Pro 12.9 inch (Cellular)"; if ([platform isEqualToString:@"i386"]) return @"Simulator"; if ([platform isEqualToString:@"x86_64"]) return @"Simulator"; return platform; } |
请随意使用这个类(gist@github)
CODE REMOVED AND RELOCATED TO
https://gist.github.com/1323251
更新(01/14/11)
显然,到目前为止,这段代码有点过时,但它肯定可以使用
我在我的应用程序中使用这个。截至2012年12月。
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 | - (NSString *) platformString { // Gets a string with the device model size_t size; sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *machine = malloc(size); sysctlbyname("hw.machine", machine, &size, NULL, 0); NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding]; free(machine); if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G"; if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4"; if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4"; if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4 (CDMA)"; if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5"; if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (GSM+CDMA)"; if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch (1 Gen)"; if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch (2 Gen)"; if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch (3 Gen)"; if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch (4 Gen)"; if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch (5 Gen)"; if ([platform isEqualToString:@"iPad1,1"]) return @"iPad"; if ([platform isEqualToString:@"iPad1,2"]) return @"iPad 3G"; if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)"; if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2"; if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)"; if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2"; if ([platform isEqualToString:@"iPad2,5"]) return @"iPad Mini (WiFi)"; if ([platform isEqualToString:@"iPad2,6"]) return @"iPad Mini"; if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini (GSM+CDMA)"; if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)"; if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (GSM+CDMA)"; if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3"; if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)"; if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4"; if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4 (GSM+CDMA)"; if ([platform isEqualToString:@"i386"]) return @"Simulator"; if ([platform isEqualToString:@"x86_64"]) return @"Simulator"; return platform; } |
1 2 3 4 5 | if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad) { //Device is ipad }else{ //Device is iphone } |
最新更新-2019年3月:新增iPad Air 2019(第3代)、iPad Mini 2019(第6代)
先前更新-2018年11月:新增iPhone XS、iPhone XS Max、iPhone XR、Apple Watch 4系列、iPad Pro 11、iPad Pro 12.9(第3代)
迅捷(在Swift 5、Swift 4、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 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 | func userDeviceName() -> String { let platform: String = { var size = 0 sysctlbyname("hw.machine", nil, &size, nil, 0) var machine = [CChar](repeating: 0, count: Int(size)) sysctlbyname("hw.machine", &machine, &size, nil, 0) return String(cString: machine) }() //iPhone if platform =="iPhone1,1" { return"iPhone (1st generation)" } else if platform =="iPhone1,2" { return"iPhone 3G" } else if platform =="iPhone2,1" { return"iPhone 3GS" } else if platform =="iPhone3,1" { return"iPhone 4 (GSM)" } else if platform =="iPhone3,2" { return"iPhone 4 (GSM, 2nd revision)" } else if platform =="iPhone3,3" { return"iPhone 4 (CDMA)" } else if platform =="iPhone4,1" { return"iPhone 4S" } else if platform =="iPhone5,1" { return"iPhone 5 (GSM)" } else if platform =="iPhone5,2" { return"iPhone 5 (GSM+CDMA)" } else if platform =="iPhone5,3" { return"iPhone 5c (GSM)" } else if platform =="iPhone5,4" { return"iPhone 5c (GSM+CDMA)" } else if platform =="iPhone6,1" { return"iPhone 5s (GSM)" } else if platform =="iPhone6,2" { return"iPhone 5s (GSM+CDMA)" } else if platform =="iPhone7,2" { return"iPhone 6" } else if platform =="iPhone7,1" { return"iPhone 6 Plus" } else if platform =="iPhone8,1" { return"iPhone 6s" } else if platform =="iPhone8,2" { return"iPhone 6s Plus" } else if platform =="iPhone8,4" { return"iPhone SE" } else if platform =="iPhone9,1" { return"iPhone 7 (GSM+CDMA)" } else if platform =="iPhone9,3" { return"iPhone 7 (GSM)" } else if platform =="iPhone9,2" { return"iPhone 7 Plus (GSM+CDMA)" } else if platform =="iPhone9,4" { return"iPhone 7 Plus (GSM)" } else if platform =="iPhone10,1" { return"iPhone 8 (GSM+CDMA)" } else if platform =="iPhone10,4" { return"iPhone 8 (GSM)" } else if platform =="iPhone10,2" { return"iPhone 8 Plus (GSM+CDMA)" } else if platform =="iPhone10,5" { return"iPhone 8 Plus (GSM)" } else if platform =="iPhone10,3" { return"iPhone X (GSM+CDMA)" } else if platform =="iPhone11,2" { return"iPhone XS" } else if platform =="iPhone11,6" { return"iPhone XS Max" } else if platform =="iPhone11,8" { return"iPhone XR" } //iPod Touch else if platform =="iPod1,1" { return"iPod Touch (1st generation)" } else if platform =="iPod2,1" { return"iPod Touch (2nd generation)" } else if platform =="iPod3,1" { return"iPod Touch (3rd generation)" } else if platform =="iPod4,1" { return"iPod Touch (4th generation)" } else if platform =="iPod5,1" { return"iPod Touch (5th generation)" } else if platform =="iPod7,1" { return"iPod Touch (6th generation)" } //iPad else if platform =="iPad1,1" { return"iPad (1st generation)" } else if platform =="iPad2,1" { return"iPad 2 (Wi-Fi)" } else if platform =="iPad2,2" { return"iPad 2 (GSM)" } else if platform =="iPad2,3" { return"iPad 2 (CDMA)" } else if platform =="iPad2,4" { return"iPad 2 (Wi-Fi, Mid 2012)" } else if platform =="iPad3,1" { return"iPad (3rd generation) (Wi-Fi)" } else if platform =="iPad3,2" { return"iPad (3rd generation) (GSM+CDMA)" } else if platform =="iPad3,3" { return"iPad (3rd generation) (GSM)" } else if platform =="iPad3,4" { return"iPad (4th generation) (Wi-Fi)"} else if platform =="iPad3,5" { return"iPad (4th generation) (GSM)" } else if platform =="iPad3,6" { return"iPad (4th generation) (GSM+CDMA)" } else if platform =="iPad6,11" { return"iPad (2017) (5th generation) (Wi-Fi)" } else if platform =="iPad6,12" { return"iPad (2017) (5th generation) (Cellular)" } else if platform =="iPad7,5" { return"iPad (2018) (6th generation) (Wi-Fi)" } else if platform =="iPad7,6" { return"iPad (2018) (6th generation) (Cellular)" } //iPad Mini else if platform =="iPad2,5" { return"iPad mini (Wi-Fi)" } else if platform =="iPad2,6" { return"iPad mini (GSM)" } else if platform =="iPad2,7" { return"iPad mini (GSM+CDMA)" } else if platform =="iPad4,4" { return"iPad mini 2 (Wi-Fi)" } else if platform =="iPad4,5" { return"iPad mini 2 (Cellular)" } else if platform =="iPad4,6" { return"iPad mini 2 (China)" } else if platform =="iPad4,7" { return"iPad mini 3 (Wi-Fi)" } else if platform =="iPad4,8" { return"iPad mini 3 (Cellular)" } else if platform =="iPad4,9" { return"iPad mini 3 (China)" } else if platform =="iPad5,1" { return"iPad mini 4 (Wi-Fi)" } else if platform =="iPad5,2" { return"iPad mini 4 (Cellular)" } else if platform =="iPad11,1" { return"iPad mini (2019) (5th generation) (Wi-Fi)" } else if platform =="iPad11,2" { return"iPad mini (2019) (5th generation) (Cellular)" } //iPad Air else if platform =="iPad4,1" { return"iPad Air (Wi-Fi)" } else if platform =="iPad4,2" { return"iPad Air (Cellular)" } else if platform =="iPad4,3" { return"iPad Air (China)" } else if platform =="iPad5,3" { return"iPad Air 2 (Wi-Fi)" } else if platform =="iPad5,4" { return"iPad Air 2 (Cellular)" } else if platform =="iPad11,3" { return"iPad Air (2019) (3rd generation) (Wi-Fi)" } else if platform =="iPad11,4" { return"iPad Air (2019) (3rd generation) (Cellular)" } //iPad Pro else if platform =="iPad6,3" { return"iPad Pro 9.7" (2016) (Wi-Fi)" } else if platform =="iPad6,4" { return"iPad Pro 9.7" (2016) (Cellular)" } else if platform =="iPad6,7" { return"iPad Pro 12.9" (2015) (Wi-Fi)" } else if platform =="iPad6,8" { return"iPad Pro 12.9" (2015) (Cellular)" } else if platform =="iPad7,1" { return"iPad Pro 12.9" (2017) (2nd generation) (Wi-Fi)" } else if platform =="iPad7,2" { return"iPad Pro 12.9" (2017) (2nd generation) (Cellular)" } else if platform =="iPad7,3" { return"iPad Pro 10.5" (2017) (Wi-Fi)" } else if platform =="iPad7,4" { return"iPad Pro 10.5" (2017) (Cellular)" } else if platform =="iPad8,1" { return"iPad Pro 11" (2018) (Wi-Fi)" } else if platform =="iPad8,2" { return"iPad Pro 11" (2018) (Wi-Fi, 1TB)" } else if platform =="iPad8,3" { return"iPad Pro 11" (2018) (Cellular)" } else if platform =="iPad8,4" { return"iPad Pro 11" (2018) (Cellular 1TB)" } else if platform =="iPad8,5" { return"iPad Pro 12.9" (2018) (3rd generation) (Wi-Fi)" } else if platform =="iPad8,6" { return"iPad Pro 12.9" (2018) (3rd generation) (Cellular)" } else if platform =="iPad8,7" { return"iPad Pro 12.9" (2018) (3rd generation) (Wi-Fi, 1TB)" } else if platform =="iPad8,8" { return"iPad Pro 12.9" (2018) (3rd generation) (Cellular, 1TB)" } //Apple TV else if platform =="AppleTV2,1" { return"Apple TV 2G" } else if platform =="AppleTV3,1" { return"Apple TV 3" } else if platform =="AppleTV3,2" { return"Apple TV 3 (2013)" } else if platform =="AppleTV5,3" { return"Apple TV 4" } else if platform =="AppleTV6,2" { return"Apple TV 4K" } //Apple Watch else if platform =="Watch1,1" { return"Apple Watch (1st generation) (38mm)" } else if platform =="Watch1,2" { return"Apple Watch (1st generation) (42mm)" } else if platform =="Watch2,6" { return"Apple Watch Series 1 (38mm)" } else if platform =="Watch2,7" { return"Apple Watch Series 1 (42mm)" } else if platform =="Watch2,3" { return"Apple Watch Series 2 (38mm)" } else if platform =="Watch2,4" { return"Apple Watch Series 2 (42mm)" } else if platform =="Watch3,1" { return"Apple Watch Series 3 (38mm Cellular)" } else if platform =="Watch3,2" { return"Apple Watch Series 3 (42mm Cellular)" } else if platform =="Watch3,3" { return"Apple Watch Series 3 (38mm)" } else if platform =="Watch3,4" { return"Apple Watch Series 3 (42mm)" } else if platform =="Watch4,1" { return"Apple Watch Series 4 (40mm)" } else if platform =="Watch4,2" { return"Apple Watch Series 4 (44mm)" } else if platform =="Watch4,3" { return"Apple Watch Series 4 (40mm Cellular)" } else if platform =="Watch4,4" { return"Apple Watch Series 4 (44mm Cellular)" } //Simulator else if platform =="i386" { return"Simulator" } else if platform =="x86_64" { return"Simulator"} return platform } |
您可以使用以下代码对其进行测试:
1 | print("Current device is:", self.userDeviceName()) |
Objtovi-C
我还为Objective-C代码添加了新设备
不要忘记导入sys/sysctl.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 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 | #import <sys/sysctl.h> - (NSString *) userDeviceName { size_t size; sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *machine = malloc(size); sysctlbyname("hw.machine", machine, &size, NULL, 0); NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding]; free(machine); //iPhone if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone (1st generation)"; if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4 (GSM)"; if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4 (GSM, 2nd revision)"; if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4 (Verizon)"; if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)"; if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (GSM)"; if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (GSM)"; if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6"; if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus"; if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s"; if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus"; if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE"; if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7 (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone9,3"]) return @"iPhone 7 (GSM)"; if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone9,4"]) return @"iPhone 7 Plus (GSM)"; if ([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8 (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone10,4"]) return @"iPhone 8 (GSM)"; if ([platform isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus (GSM)"; if ([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone10,6"]) return @"iPhone X (GSM)"; if ([platform isEqualToString:@"iPhone11,2"]) return @"iPhone XS"; if ([platform isEqualToString:@"iPhone11,6"]) return @"iPhone XS Max"; if ([platform isEqualToString:@"iPhone11,8"]) return @"iPhone XR"; //iPod Touch if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch (1st generation)"; if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch (2nd generation)"; if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch (3rd generation)"; if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch (4th generation)"; if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch (5th generation)"; if ([platform isEqualToString:@"iPod7,1"]) return @"iPod Touch (6th generation)"; //iPad if ([platform isEqualToString:@"iPad1,1"]) return @"iPad (1st generation)"; if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (Wi-Fi)"; if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)"; if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)"; if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2 (Wi-Fi, Mid 2012)"; if ([platform isEqualToString:@"iPad2,5"]) return @"iPad mini (Wi-Fi)"; if ([platform isEqualToString:@"iPad2,6"]) return @"iPad mini (GSM)"; if ([platform isEqualToString:@"iPad2,7"]) return @"iPad mini (GSM+CDMA)"; if ([platform isEqualToString:@"iPad3,1"]) return @"iPad (3rd generation) (Wi-Fi)"; if ([platform isEqualToString:@"iPad3,2"]) return @"iPad (3rd generation) (GSM+CDMA)"; if ([platform isEqualToString:@"iPad3,3"]) return @"iPad (3rd generation) (GSM)"; if ([platform isEqualToString:@"iPad3,4"]) return @"iPad (4th generation) (Wi-Fi)"; if ([platform isEqualToString:@"iPad3,5"]) return @"iPad (4th generation) (GSM)"; if ([platform isEqualToString:@"iPad3,6"]) return @"iPad (4th generation) (GSM+CDMA)"; if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air (Wi-Fi)"; if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air (Cellular)"; if ([platform isEqualToString:@"iPad4,3"]) return @"iPad Air (China)"; if ([platform isEqualToString:@"iPad4,4"]) return @"iPad mini 2 (Wi-Fi)"; if ([platform isEqualToString:@"iPad4,5"]) return @"iPad mini 2 (Cellular)"; if ([platform isEqualToString:@"iPad4,6"]) return @"iPad mini 2 (China)"; if ([platform isEqualToString:@"iPad4,7"]) return @"iPad mini 3 (Wi-Fi)"; if ([platform isEqualToString:@"iPad4,8"]) return @"iPad mini 3 (Cellular)"; if ([platform isEqualToString:@"iPad4,9"]) return @"iPad mini 3 (China)"; if ([platform isEqualToString:@"iPad5,1"]) return @"iPad mini 4 (Wi-Fi)"; if ([platform isEqualToString:@"iPad5,2"]) return @"iPad mini 4 (Cellular)"; if ([platform isEqualToString:@"iPad5,3"]) return @"iPad Air 2 (Wi-Fi)"; if ([platform isEqualToString:@"iPad5,4"]) return @"iPad Air 2 (Cellular)"; if ([platform isEqualToString:@"iPad6,3"]) return @"iPad Pro 9.7" (2016) (Wi-Fi)"; if ([platform isEqualToString:@"iPad6,4"]) return @"iPad Pro 9.7" (2016) (Cellular)"; if ([platform isEqualToString:@"iPad6,7"]) return @"iPad Pro 12.9" (2015) (Wi-Fi)"; if ([platform isEqualToString:@"iPad6,8"]) return @"iPad Pro 12.9" (2015) (Cellular)"; if ([platform isEqualToString:@"iPad6,11"]) return @"iPad (2017) (5th generation) (Wi-Fi)"; if ([platform isEqualToString:@"iPad6,12"]) return @"iPad (2017) (5th generation) (Cellular)"; if ([platform isEqualToString:@"iPad7,1"]) return @"iPad Pro 12.9" (2017) (2nd generation) (Wi-Fi)"; if ([platform isEqualToString:@"iPad7,2"]) return @"iPad Pro 12.9" (2017) (2nd generation) (Cellular)"; if ([platform isEqualToString:@"iPad7,3"]) return @"iPad Pro 10.5" (2017) (Wi-Fi)"; if ([platform isEqualToString:@"iPad7,4"]) return @"iPad Pro 10.5" (2017) (Cellular)"; if ([platform isEqualToString:@"iPad7,5"]) return @"iPad (2018) (6th generation) (Wi-Fi)"; if ([platform isEqualToString:@"iPad7,6"]) return @"iPad (2018) (6th generation) (Cellular)"; if ([platform isEqualToString:@"iPad8,1"]) return @"iPad Pro 11" (2018) (Wi-Fi)"; if ([platform isEqualToString:@"iPad8,2"]) return @"iPad Pro 11" (2018) (Wi-Fi, 1TB)"; if ([platform isEqualToString:@"iPad8,3"]) return @"iPad Pro 11" (2018) (Wi-Fi)"; if ([platform isEqualToString:@"iPad8,4"]) return @"iPad Pro 11" (2018) (Cellular)"; if ([platform isEqualToString:@"iPad8,5"]) return @"iPad Pro 12.9" (2018) (3rd generation) (Wi-Fi)"; if ([platform isEqualToString:@"iPad8,6"]) return @"iPad Pro 12.9" (2018) (3rd generation) (Cellular)"; if ([platform isEqualToString:@"iPad8,7"]) return @"iPad Pro 12.9" (2018) (3rd generation) (Wi-Fi, 1TB)"; if ([platform isEqualToString:@"iPad8,8"]) return @"iPad Pro 12.9" (2018) (3rd generation) (Cellular, 1TB)"; if ([platform isEqualToString:@"iPad11,1"]) return @"iPad mini (2019) (5th generation) (Wi-Fi)"; if ([platform isEqualToString:@"iPad11,2"]) return @"iPad mini (2019) (5th generation) (Cellular)"; if ([platform isEqualToString:@"iPad11,3"]) return @"iPad Air (2019) (3rd generation) (Wi-Fi)"; if ([platform isEqualToString:@"iPad11,4"]) return @"iPad Air (2019) (3rd generation) (Cellular)"; //Apple TV if ([platform isEqualToString:@"AppleTV2,1"]) return @"Apple TV 2G"; if ([platform isEqualToString:@"AppleTV3,1"]) return @"Apple TV 3"; if ([platform isEqualToString:@"AppleTV3,2"]) return @"Apple TV 3 (2013)"; if ([platform isEqualToString:@"AppleTV5,3"]) return @"Apple TV 4"; if ([platform isEqualToString:@"AppleTV6,2"]) return @"Apple TV 4K"; //Apple Watch if ([platform isEqualToString:@"Watch1,1"]) return @"Apple Watch (1st generation) (38mm)"; if ([platform isEqualToString:@"Watch1,2"]) return @"Apple Watch (1st generation) (42mm)"; if ([platform isEqualToString:@"Watch2,6"]) return @"Apple Watch Series 1 (38mm)"; if ([platform isEqualToString:@"Watch2,7"]) return @"Apple Watch Series 1 (42mm)"; if ([platform isEqualToString:@"Watch2,3"]) return @"Apple Watch Series 2 (38mm)"; if ([platform isEqualToString:@"Watch2,4"]) return @"Apple Watch Series 2 (42mm)"; if ([platform isEqualToString:@"Watch3,1"]) return @"Apple Watch Series 3 (38mm Cellular)"; if ([platform isEqualToString:@"Watch3,2"]) return @"Apple Watch Series 3 (42mm Cellular)"; if ([platform isEqualToString:@"Watch3,3"]) return @"Apple Watch Series 3 (38mm)"; if ([platform isEqualToString:@"Watch3,4"]) return @"Apple Watch Series 3 (42mm)"; if ([platform isEqualToString:@"Watch4,1"]) return @"Apple Watch Series 3 (40mm)"; if ([platform isEqualToString:@"Watch4,2"]) return @"Apple Watch Series 3 (44mm)"; if ([platform isEqualToString:@"Watch4,3"]) return @"Apple Watch Series 3 (40mm Cellular)"; if ([platform isEqualToString:@"Watch4,4"]) return @"Apple Watch Series 3 (44mm Cellular)"; //Simulator if ([platform isEqualToString:@"i386"]) return @"Simulator"; if ([platform isEqualToString:@"x86_64"]) return @"Simulator"; return platform; } |
您可以使用以下代码对其进行测试:
1 | NSLog(@"Current device is: %@", [self userDeviceName]); |
更新了
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 | - (NSString *)platformString { NSString *platform = [self platform]; if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G"; if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4"; if ([platform isEqualToString:@"iPhone3,3"]) return @"Verizon iPhone 4"; if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)"; if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (GSM)"; if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (GSM)"; if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus"; if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6"; if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G"; if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G"; if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G"; if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G"; if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G"; if ([platform isEqualToString:@"iPad1,1"]) return @"iPad"; if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)"; if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)"; if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)"; if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2 (WiFi)"; if ([platform isEqualToString:@"iPad2,5"]) return @"iPad Mini (WiFi)"; if ([platform isEqualToString:@"iPad2,6"]) return @"iPad Mini (GSM)"; if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini (GSM+CDMA)"; if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)"; if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (GSM+CDMA)"; if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3 (GSM)"; if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)"; if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4 (GSM)"; if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4 (GSM+CDMA)"; if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air (WiFi)"; if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air (Cellular)"; if ([platform isEqualToString:@"iPad4,4"]) return @"iPad mini 2G (WiFi)"; if ([platform isEqualToString:@"iPad4,5"]) return @"iPad mini 2G (Cellular)"; if ([platform isEqualToString:@"iPad4,7"]) return @"iPad mini 3 (WiFi)"; if ([platform isEqualToString:@"iPad4,8"]) return @"iPad mini 3 (Cellular)"; if ([platform isEqualToString:@"iPad4,9"]) return @"iPad mini 3 (China Model)"; if ([platform isEqualToString:@"iPad5,3"]) return @"iPad Air 2 (WiFi)"; if ([platform isEqualToString:@"iPad5,4"]) return @"iPad Air 2 (Cellular)"; if ([platform isEqualToString:@"iPad6,8"]) return @"iPad Pro"; if ([platform isEqualToString:@"i386"]) return @"Simulator"; if ([platform isEqualToString:@"x86_64"]) return @"Simulator"; return platform; } |
更有用
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 | #include <sys/types.h> #include <sys/sysctl.h> @interface UIDevice(Hardware) - (NSString *) platform; - (BOOL)hasRetinaDisplay; - (BOOL)hasMultitasking; - (BOOL)hasCamera; @end @implementation UIDevice(Hardware) - (NSString *) platform{ int mib[2]; size_t len; char *machine; mib[0] = CTL_HW; mib[1] = HW_MACHINE; sysctl(mib, 2, NULL, &len, NULL, 0); machine = malloc(len); sysctl(mib, 2, machine, &len, NULL, 0); NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding]; free(machine); return platform; } - (BOOL)hasRetinaDisplay { NSString *platform = [self platform]; BOOL ret = YES; if ([platform isEqualToString:@"iPhone1,1"]) { ret = NO; } else if ([platform isEqualToString:@"iPhone1,2"]) ret = NO; else if ([platform isEqualToString:@"iPhone2,1"]) ret = NO; else if ([platform isEqualToString:@"iPod1,1"]) ret = NO; else if ([platform isEqualToString:@"iPod2,1"]) ret = NO; else if ([platform isEqualToString:@"iPod3,1"]) ret = NO; return ret; } - (BOOL)hasMultitasking { if ([self respondsToSelector:@selector(isMultitaskingSupported)]) { return [self isMultitaskingSupported]; } return NO; } - (BOOL)hasCamera { BOOL ret = NO; // check camera availability return ret; } @end |
可以使用读取属性
1 | NSLog(@"platform %@, retita %@, multitasking %@", [[UIDevice currentDevice] platform], [[UIDevice currentDevice] hasRetinaDisplay] ? @"YES" : @"NO" , [[UIDevice currentDevice] hasMultitasking] ? @"YES" : @"NO"); |
我知道答案已经勾选了,但为了将来参考,您可以始终使用设备屏幕大小来确定它是什么样的设备:
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 | if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { CGSize result = [[UIScreen mainScreen] bounds].size; if (result.height == 480) { // 3.5 inch display - iPhone 4S and below NSLog(@"Device is an iPhone 4S or below"); } else if (result.height == 568) { // 4 inch display - iPhone 5 NSLog(@"Device is an iPhone 5/S/C"); } else if (result.height == 667) { // 4.7 inch display - iPhone 6 NSLog(@"Device is an iPhone 6"); } else if (result.height == 736) { // 5.5 inch display - iPhone 6 Plus NSLog(@"Device is an iPhone 6 Plus"); } } else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { // iPad 9.7 or 7.9 inch display. NSLog(@"Device is an iPad."); } |
以下是新型号的一个小更新:
1 2 3 4 5 6 7 8 9 10 11 12 | - (NSString *) platformString{ NSString *platform = [self platform]; if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G"; if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4"; if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G"; if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G"; if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G"; if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator"; return platform; } |
只需将iPhone4S设备代码添加到此线程中…
iPhone4S将返回字符串@"iphone4,1"。
这个代码怎么样,如果发布了新版本,您将用最后一个已知设备进行标识
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 | - (NSString *)getModel { size_t size; sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *model = malloc(size); sysctlbyname("hw.machine", model, &size, NULL, 0); NSString *sDeviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding]; free(model); if ([sDeviceModel isEqual:@"i386"]) return @"Simulator"; //iPhone Simulator if ([sDeviceModel isEqual:@"iPhone1,1"]) return @"iPhone1G"; //iPhone 1G if ([sDeviceModel isEqual:@"iPhone1,2"]) return @"iPhone3G"; //iPhone 3G if ([sDeviceModel isEqual:@"iPhone2,1"]) return @"iPhone3GS"; //iPhone 3GS if ([sDeviceModel isEqual:@"iPhone3,1"]) return @"iPhone3GS"; //iPhone 4 - AT&T if ([sDeviceModel isEqual:@"iPhone3,2"]) return @"iPhone3GS"; //iPhone 4 - Other carrier if ([sDeviceModel isEqual:@"iPhone3,3"]) return @"iPhone4"; //iPhone 4 - Other carrier if ([sDeviceModel isEqual:@"iPhone4,1"]) return @"iPhone4S"; //iPhone 4S if ([sDeviceModel isEqual:@"iPod1,1"]) return @"iPod1stGen"; //iPod Touch 1G if ([sDeviceModel isEqual:@"iPod2,1"]) return @"iPod2ndGen"; //iPod Touch 2G if ([sDeviceModel isEqual:@"iPod3,1"]) return @"iPod3rdGen"; //iPod Touch 3G if ([sDeviceModel isEqual:@"iPod4,1"]) return @"iPod4thGen"; //iPod Touch 4G if ([sDeviceModel isEqual:@"iPad1,1"]) return @"iPadWiFi"; //iPad Wifi if ([sDeviceModel isEqual:@"iPad1,2"]) return @"iPad3G"; //iPad 3G if ([sDeviceModel isEqual:@"iPad2,1"]) return @"iPad2"; //iPad 2 (WiFi) if ([sDeviceModel isEqual:@"iPad2,2"]) return @"iPad2"; //iPad 2 (GSM) if ([sDeviceModel isEqual:@"iPad2,3"]) return @"iPad2"; //iPad 2 (CDMA) NSString *aux = [[sDeviceModel componentsSeparatedByString:@","] objectAtIndex:0]; //If a newer version exist if ([aux rangeOfString:@"iPhone"].location!=NSNotFound) { int version = [[aux stringByReplacingOccurrencesOfString:@"iPhone" withString:@""] intValue]; if (version == 3) return @"iPhone4" if (version >= 4) return @"iPhone4s"; } if ([aux rangeOfString:@"iPod"].location!=NSNotFound) { int version = [[aux stringByReplacingOccurrencesOfString:@"iPod" withString:@""] intValue]; if (version >=4) return @"iPod4thGen"; } if ([aux rangeOfString:@"iPad"].location!=NSNotFound) { int version = [[aux stringByReplacingOccurrencesOfString:@"iPad" withString:@""] intValue]; if (version ==1) return @"iPad3G"; if (version >=2) return @"iPad2"; } //If none was found, send the original string return sDeviceModel; } |
添加到Arash的代码中,我不关心我的应用程序使用的是什么型号,我只是想知道什么类型的设备,所以我可以测试如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { NSLog(@"I'm definitely an iPad"); } else { NSString *deviceType = [UIDevice currentDevice].model; if([deviceType rangeOfString:@"iPhone"].location!=NSNotFound) { NSLog(@"I must be an iPhone"); } else { NSLog(@"I think I'm an iPod"); } } |
基于以上非常好的答案,下面是我的想法。这与@rodrigo的答案非常相似,但从对该答案的评论中可以看出@oliver的担忧。这还提供了在输出字符串中包含模型字符串的选项。
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 | + (NSString *) deviceModel { 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; } + (NSString *) deviceName { NSString *deviceModel = [DeviceGateway deviceModel]; if ([deviceModel isEqual:@"i386"]) return @"Simulator"; //iPhone Simulator if ([deviceModel isEqual:@"iPhone1,1"]) return @"iPhone1G"; //iPhone 1G if ([deviceModel isEqual:@"iPhone1,2"]) return @"iPhone3G"; //iPhone 3G if ([deviceModel isEqual:@"iPhone2,1"]) return @"iPhone3GS"; //iPhone 3GS if ([deviceModel isEqual:@"iPhone3,1"]) return @"iPhone4"; //iPhone 4 - AT&T if ([deviceModel isEqual:@"iPhone3,2"]) return @"iPhone4"; //iPhone 4 - Other carrier if ([deviceModel isEqual:@"iPhone3,3"]) return @"iPhone4"; //iPhone 4 - Other carrier if ([deviceModel isEqual:@"iPhone4,1"]) return @"iPhone4S"; //iPhone 4S if ([deviceModel isEqual:@"iPod1,1"]) return @"iPod1stGen"; //iPod Touch 1G if ([deviceModel isEqual:@"iPod2,1"]) return @"iPod2ndGen"; //iPod Touch 2G if ([deviceModel isEqual:@"iPod3,1"]) return @"iPod3rdGen"; //iPod Touch 3G if ([deviceModel isEqual:@"iPod4,1"]) return @"iPod4thGen"; //iPod Touch 4G if ([deviceModel isEqual:@"iPad1,1"]) return @"iPadWiFi"; //iPad Wifi if ([deviceModel isEqual:@"iPad1,2"]) return @"iPad3G"; //iPad 3G if ([deviceModel isEqual:@"iPad2,1"]) return @"iPad2"; //iPad 2 (WiFi) if ([deviceModel isEqual:@"iPad2,2"]) return @"iPad2"; //iPad 2 (GSM) if ([deviceModel isEqual:@"iPad2,3"]) return @"iPad2"; //iPad 2 (CDMA) NSString *aux = [[deviceModel componentsSeparatedByString:@","] objectAtIndex:0]; //If a newer version exists if ([aux rangeOfString:@"iPhone"].location != NSNotFound) { int version = [[aux stringByReplacingOccurrencesOfString:@"iPhone" withString:@""] intValue]; if (version == 3) return @"iPhone4"; if (version == 4) return @"iPhone4s"; return @"Newer iPhone"; } if ([aux rangeOfString:@"iPod"].location != NSNotFound) { int version = [[aux stringByReplacingOccurrencesOfString:@"iPod" withString:@""] intValue]; if (version == 4) return @"iPod4thGen"; return @"Newer iPod"; } if ([aux rangeOfString:@"iPad"].location != NSNotFound) { int version = [[aux stringByReplacingOccurrencesOfString:@"iPad" withString:@""] intValue]; if (version == 1) return @"iPad3G"; if (version == 2) return @"iPad2"; return @"Newer iPad"; } //If none was found, send the original string return deviceModel; } + (NSString *) deviceNameWithDeviceModel:(BOOL)shouldIncludeDeviceModel { if (shouldIncludeDeviceModel) { return [NSString stringWithFormat:@"%@ (%@)", [DeviceGateway deviceName], [DeviceGateway deviceModel]]; } return [DeviceGateway deviceName]; } |
有一个相当成熟的库(由我负责),叫做sdversion。您可以检查运行设备的型号、屏幕大小和许多其他参数。它还支持OSX。
例子:
1 2 3 4 5 6 7 8 9 10 11 | // Check for device model if ([SDVersion deviceVersion] == iPhone6) NSLog(@"You got the iPhone 6. Sweet ??!"); else if ([SDVersion deviceVersion] == iPhone6Plus) NSLog(@"iPhone 6 Plus? Bigger is better!"); else if ([SDVersion deviceVersion] == iPadAir2) NSLog(@"You own an iPad Air 2 ??!"); // Check for device screen size if ([SDVersion deviceSize] == Screen4inch) NSLog(@"Your screen is 4 inches"); |
1 | NSString *deviceType = [UIDevice currentDevice].model; |
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 | #import <sys/utsname.h> NSString *iPhone = [self iPhoneVersion]; -(NSString*)iPhoneVersion { NSString *platform; struct utsname systemInfo; uname(&systemInfo); platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G"; if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4"; if ([platform isEqualToString:@"iPhone3,3"]) return @"VerizoniPhone 4"; if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)"; if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (GSM)"; if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (GSM)"; if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6"; if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus"; if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G"; if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G"; if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G"; if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G"; if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G"; if ([platform isEqualToString:@"iPad1,1"]) return @"iPad"; if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)"; if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)"; if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)"; if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2 (WiFi)"; if ([platform isEqualToString:@"iPad2,5"]) return @"iPad Mini (WiFi)"; if ([platform isEqualToString:@"iPad2,6"]) return @"iPad Mini (GSM)"; if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini (GSM+CDMA)"; if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)"; if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (GSM+CDMA)"; if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3 (GSM)"; if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)"; if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4 (GSM)"; if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4 (GSM+CDMA)"; if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air (WiFi)"; if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air (Cellular)"; if ([platform isEqualToString:@"iPad4,3"]) return @"iPad Air"; if ([platform isEqualToString:@"iPad4,4"]) return @"iPad Mini 2G (WiFi)"; if ([platform isEqualToString:@"iPad4,5"]) return @"iPad Mini 2G (Cellular)"; if ([platform isEqualToString:@"iPad4,6"]) return @"iPad Mini 2G"; if ([platform isEqualToString:@"iPad4,7"]) return @"iPad Mini 3 (WiFi)"; if ([platform isEqualToString:@"iPad4,8"]) return @"iPad Mini 3 (Cellular)"; if ([platform isEqualToString:@"iPad4,9"]) return @"iPad Mini 3 (China)"; if ([platform isEqualToString:@"iPad5,3"]) return @"iPad Air 2 (WiFi)"; if ([platform isEqualToString:@"iPad5,4"]) return @"iPad Air 2 (Cellular)"; if ([platform isEqualToString:@"AppleTV2,1"]) return @"Apple TV 2G"; if ([platform isEqualToString:@"AppleTV3,1"]) return @"Apple TV 3"; if ([platform isEqualToString:@"AppleTV3,2"]) return @"Apple TV 3 (2013)"; if ([platform isEqualToString:@"i386"]) return @"Simulator"; if ([platform isEqualToString:@"x86_64"]) return @"Simulator"; return platform; } |
我更进一步,将这个大的"isEqualToString"块转换为设备类型、代和逗号后面的其他限定符(我称之为子代)的位掩码分类。它被包装在一个带有单例调用SGPlatform的类中,这样可以避免大量重复的字符串操作。代码可用https://github.com/danloughney/spokygroup
该类允许您执行以下操作:
1 2 3 | if ([SGPlatform iPad] && [SGPlatform generation] > 3) { // set for high performance } |
和
1 2 3 4 5 6 7 8 | switch ([SGPlatform deviceMask]) { case DEVICE_IPHONE: break; case DEVICE_IPAD: break; case DEVICE_IPAD_MINI: break; } |
设备的分类采用platformbits方法。这个方法对于这个线程的读者应该非常熟悉。我已经按设备的类型和代对其进行了分类,因为我对整体性能最感兴趣,但是可以调整源以提供您感兴趣的任何分类、视网膜屏幕、网络功能等。
下面提到的代码段应该有帮助:
1 2 3 4 5 6 7 8 9 | if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { // iPhone device } else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { // iPad device } else { // Other device i.e. iPod } |
1 2 3 4 5 6 7 8 | - (BOOL)deviceiPhoneOriPod { NSString *deviceType = [UIDevice currentDevice].model; if([deviceType rangeOfString:@"iPhone"].location!=NSNotFound) return YES; else return NO; } |
我喜欢艾丽卡·萨顿的作品。她包括AppleTV设备和其他你可能不会想到的设备。
https://github.com/erica/uidevice扩展/blob/master/uidevice硬件.h
1 | if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4"; //Verizon |
用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 | struct DeviceInformation { // UIDevice.current.model's value is"iPhone" or"iPad",which does not include details; the following"model" is detailed, such as, iPhone7,1 is actually iPhone 6 plus static let model: String = { var size = 0 sysctlbyname("hw.machine", nil, &size, nil, 0) var machine = [CChar](repeating: 0, count: Int(size)) sysctlbyname("hw.machine", &machine, &size, nil, 0) return String(cString: machine) }() static let uuid = UIDevice.current.identifierForVendor?.uuidString ??"" static let idForAdService = ASIdentifierManager.shared().advertisingIdentifier.uuidString static func diviceTypeReadableName() -> String { switch model { case"iPhone1,1": return"iPhone 1G" case"iPhone1,2": return"iPhone 3G" case"iPhone2,1": return"iPhone 3GS" case"iPhone3,1": return"iPhone 4" case"iPhone3,3": return"iPhone 4 (Verizon)" case"iPhone4,1": return"iPhone 4S" case"iPhone5,1": return"iPhone 5 (GSM)" case"iPhone5,2": return"iPhone 5 (GSM+CDMA)" case"iPhone5,3": return"iPhone 5c (GSM)" case"iPhone5,4": return"iPhone 5c (GSM+CDMA)" case"iPhone6,1": return"iPhone 5s (GSM)" case"iPhone6,2": return"iPhone 5s (GSM+CDMA)" case"iPhone7,2": return"iPhone 6" case"iPhone7,1": return"iPhone 6 Plus" case"iPhone8,1": return"iPhone 6s" case"iPhone8,2": return"iPhone 6s Plus" case"iPhone8,4": return"iPhone SE" case"iPhone9,1": return"iPhone 7" case"iPhone9,3": return"iPhone 7" case"iPhone9,2": return"iPhone 7 Plus" case"iPhone9,4": return"iPhone 7 Plus" case"iPod1,1": return"iPod Touch 1G" case"iPod2,1": return"iPod Touch 2G" case"iPod3,1": return"iPod Touch 3G" case"iPod4,1": return"iPod Touch 4G" case"iPod5,1": return"iPod Touch 5G" case"iPod7,1": return"iPod Touch 6G" case"iPad1,1": return"iPad 1G" case"iPad2,1": return"iPad 2 (Wi-Fi)" case"iPad2,2": return"iPad 2 (GSM)" case"iPad2,3": return"iPad 2 (CDMA)" case"iPad2,4": return"iPad 2 (Wi-Fi)" case"iPad2,5": return"iPad Mini (Wi-Fi)" case"iPad2,6": return"iPad Mini (GSM)" case"iPad2,7": return"iPad Mini (GSM+CDMA)" case"iPad3,1": return"iPad 3 (Wi-Fi)" case"iPad3,2": return"iPad 3 (GSM+CDMA)" case"iPad3,3": return"iPad 3 (GSM)" case"iPad3,4": return"iPad 4 (Wi-Fi)" case"iPad3,5": return"iPad 4 (GSM)" case"iPad3,6": return"iPad 4 (GSM+CDMA)" case"iPad4,1": return"iPad Air (Wi-Fi)" case"iPad4,2": return"iPad Air (Cellular)" case"iPad4,3": return"iPad Air (China)" case"iPad4,4": return"iPad Mini 2G (Wi-Fi)" case"iPad4,5": return"iPad Mini 2G (Cellular)" case"iPad4,6": return"iPad Mini 2G (China)" case"iPad4,7": return"iPad Mini 3 (Wi-Fi)" case"iPad4,8": return"iPad Mini 3 (Cellular)" case"iPad4,9": return"iPad Mini 3 (China)" case"iPad5,1": return"iPad Mini 4 (Wi-Fi)" case"iPad5,2": return"iPad Mini 4 (Cellular)" case"iPad5,3": return"iPad Air 2 (Wi-Fi)" case"iPad5,4": return"iPad Air 2 (Cellular)" case"iPad6,3": return"iPad Pro 9.7' (Wi-Fi)" case"iPad6,4": return"iPad Pro 9.7' (Cellular)" case"iPad6,7": return"iPad Pro 12.9' (Wi-Fi)" case"iPad6,8": return"iPad Pro 12.9' (Cellular)" case"AppleTV2,1": return"Apple TV 2G" case"AppleTV3,1": return"Apple TV 3" case"AppleTV3,2": return"Apple TV 3 (2013)" case"AppleTV5,3": return"Apple TV 4" case"Watch1,1": return"Apple Watch Series 1 (38mm, S1)" case"Watch1,2": return"Apple Watch Series 1 (42mm, S1)" case"Watch2,6": return"Apple Watch Series 1 (38mm, S1P)" case"Watch2,7": return"Apple Watch Series 1 (42mm, S1P)" case"Watch2,3": return"Apple Watch Series 2 (38mm, S2)" case"Watch2,4": return"Apple Watch Series 2 (42mm, S2)" case"i386": return"Simulator" case"x86_64": return"Simulator" default: return"" } } } |
对于简单的比较,我总是喜欢宏:
1 2 3 | #define IS_IPOD [[UIDevice currentDevice].model containsString:@"iPod"] #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) |
它简单易用。
我已经创建了一个扩展,允许您具有三个级别的标识:
它使用最新的iOS设备进行更新
uidevicedetector(设备检测器)
1 | NSString *deviceType = [[UIDevice currentDevice] systemName]; |
我可以保证,上述建议将在iOS 7及以上版本中使用。我相信iOS 6也可以。但我不确定。
您可以在GitHub上查看gbDeviceInfo,也可以通过cocoapods获得。它为检测各种属性提供了简单的API,并支持所有最新设备:
- 设备族
- 设备模型
有关更多信息,请参阅自述文件。
可能的山谷
1 | [[UIDevice currentDevice] model]; |
是
如果你想知道哪个硬件的
注意:下面的代码可能不包含所有设备的字符串,我和其他人在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; } |
要识别iPhone 4S,只需检查以下各项:
1 2 3 4 5 6 7 8 | var isIphone4S: Bool { let width = UIScreen.main.bounds.size.width let height = UIScreen.main.bounds.size.height let proportions = width > height ? width / height : height / width return proportions == 1.5 && UIDevice.current.model =="iPhone" } |
我想补充一下,为了检索设备的正面和外壳颜色,有一个专用的API:
1 2 3 4 5 | UIDevice *device = [UIDevice currentDevice]; SEL selector = NSSelectorFromString([device.systemVersion hasPrefix:@"7"] ? @"_deviceInfoForKey:" : @"deviceInfoForKey:"); if ([device respondsToSelector:selector]) { NSLog(@"DeviceColor: %@ DeviceEnclosureColor: %@", [device performSelector:selector withObject:@"DeviceColor"], [device performSelector:selector withObject:@"DeviceEnclosureColor"]); } |
我在博客上发布了这一点,并提供了一个示例应用程序:
更新平台字符串。
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 | // Created from this thread: https://gist.github.com/Jaybles/1323251 // Apple TV if ([platform isEqualToString:@"AppleTV2,1"]) return @"Apple TV 2G"; if ([platform isEqualToString:@"AppleTV3,1"]) return @"Apple TV 3G"; if ([platform isEqualToString:@"AppleTV3,2"]) return @"Apple TV 3G"; if ([platform isEqualToString:@"AppleTV5,3"]) return @"Apple TV 4G"; // Apple Watch if ([platform isEqualToString:@"Watch1,1"]) return @"Apple Watch"; if ([platform isEqualToString:@"Watch1,2"]) return @"Apple Watch"; // iPhone if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G"; if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4 (GSM)"; if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4 (GSM/2012)"; if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4 (CDMA)"; if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)"; if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (Global)"; if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (GSM)"; if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (Global)"; if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (GSM)"; if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (Global)"; if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6"; if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus"; if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s"; if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus"; if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE"; if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7 (Global)"; if ([platform isEqualToString:@"iPhone9,3"]) return @"iPhone 7 (GSM)"; if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus (Global)"; if ([platform isEqualToString:@"iPhone9,4"]) return @"iPhone 7 Plus (GSM)"; // iPad if ([platform isEqualToString:@"iPad1,1"]) return @"iPad 1"; if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)"; if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)"; if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)"; if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2 (Mid 2012)"; if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)"; if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (CDMA)"; if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3 (GSM)"; if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)"; if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4 (GSM)"; if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4 (Global)"; // iPad Air if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air (WiFi)"; if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air (Cellular)"; if ([platform isEqualToString:@"iPad4,3"]) return @"iPad Air (China)"; if ([platform isEqualToString:@"iPad5,3"]) return @"iPad Air 2 (WiFi)"; if ([platform isEqualToString:@"iPad5,4"]) return @"iPad Air 2 (Cellular)"; // iPad Mini if ([platform isEqualToString:@"iPad2,5"]) return @"iPad Mini (WiFi)"; if ([platform isEqualToString:@"iPad2,6"]) return @"iPad Mini (GSM)"; if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini (Global)"; if ([platform isEqualToString:@"iPad4,4"]) return @"iPad Mini 2 (WiFi)"; if ([platform isEqualToString:@"iPad4,5"]) return @"iPad Mini 2 (Cellular)"; if ([platform isEqualToString:@"iPad4,6"]) return @"iPad Mini 2 (China)"; if ([platform isEqualToString:@"iPad4,7"]) return @"iPad Mini 3 (WiFi)"; if ([platform isEqualToString:@"iPad4,8"]) return @"iPad Mini 3 (Cellular)"; if ([platform isEqualToString:@"iPad4,9"]) return @"iPad Mini 3 (China)"; if ([platform isEqualToString:@"iPad5,1"]) return @"iPad Mini 4 (WiFi)"; if ([platform isEqualToString:@"iPad5,2"]) return @"iPad Mini 4 (Cellular)"; // iPad Pro if ([platform isEqualToString:@"iPad6,3"]) return @"iPad Pro (9.7 inch/WiFi)"; if ([platform isEqualToString:@"iPad6,4"]) return @"iPad Pro (9.7 inch/Cellular)"; if ([platform isEqualToString:@"iPad6,7"]) return @"iPad Pro (12.9 inch/WiFi)"; if ([platform isEqualToString:@"iPad6,8"]) return @"iPad Pro (12.9 inch/Cellular)"; // iPod if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G"; if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G"; if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G"; if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G"; if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G"; if ([platform isEqualToString:@"iPod7,1"]) return @"iPod Touch 6G"; // Simulator if ([platform isEqualToString:@"i386"]) return @"Simulator"; if ([platform isEqualToString:@"x86_64"]) return @"Simulator"; |