我正在使用以下代码获取设备名称
NSString* code = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
其结果如下并映射到设备
@“iPhone9,1”:@“iPhone 7”,
@“iPhone9,3”:@“iPhone 7”,
@“iPhone9,2”:@“iPhone 7 Plus”,
@“iPhone9,4”:@“iPhone 7 Plus”,
我们是否有新发布的设备代码(iPhone 8,iPhone 8 Plus和iPhone X)
答案 0 :(得分:8)
编辑:这已得到纠正。 iPhone X 不 iPhone10,5和iPhone10,6。
iPhone10,1
& iPhone10,4
iPhone10,2
& iPhone10,5
iPhone10,3
& iPhone10,6
答案 1 :(得分:3)
import Foundation
import SystemConfiguration
class DeviceInfo {
static func platform() -> String? {
var systemInfo = utsname()
uname(&systemInfo)
let modelCode = withUnsafePointer(to: &systemInfo.machine) {
$0.withMemoryRebound(to: CChar.self, capacity: 1) {
ptr in String.init(validatingUTF8: ptr)
}
}
return modelCode
}
static func deviceType() -> String {
if let platform = platform(), let device = deviceDict[platform] {
return device
}
return "unknown"
}
static let deviceDict: Dictionary<String, String> = [
"iPhone1,1" : "iPhone (1st generation)",
"iPhone1,2" : "iPhone 3G",
"iPhone2,1" : "iPhone 3GS",
"iPhone3,1" : "iPhone 4 (GSM)",
"iPhone3,2" : "iPhone 4 (GSM, 2nd revision)",
"iPhone3,3" : "iPhone 4 (Verizon)",
"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,2" : "iPhone 6",
"iPhone7,1" : "iPhone 6 Plus",
"iPhone8,1" : "iPhone 6s",
"iPhone8,2" : "iPhone 6s Plus",
"iPhone8,4" : "iPhone SE",
"iPhone9,1" : "iPhone 7 (GSM+CDMA)",
"iPhone9,3" : "iPhone 7 (GSM)",
"iPhone9,2" : "iPhone 7 Plus (GSM+CDMA)",
"iPhone9,4" : "iPhone 7 Plus (GSM)",
"iPhone10,1" : "iPhone 8 (GSM+CDMA)",
"iPhone10,4" : "iPhone 8 (GSM)",
"iPhone10,2" : "iPhone 8 Plus (GSM+CDMA)",
"iPhone10,5" : "iPhone 8 Plus (GSM)",
"iPhone10,3" : "iPhone X (GSM+CDMA)",
"iPhone10,6" : "iPhone X (GSM)",
"iPhone11,1" : "iPhone XR (GSM+CDMA)",
"iPhone11,2" : "iPhone XS (GSM+CDMA)",
"iPhone11,3" : "iPhone XS (GSM)",
"iPhone11,4" : "iPhone XS Max (GSM+CDMA)",
"iPhone11,5" : "iPhone XS Max (GSM, Dual Sim, China)",
"iPhone11,6" : "iPhone XS Max (GSM)",
"iPhone11,8" : "iPhone XR (GSM)",
"iPod1,1" : "iPod Touch 1G",
"iPod2,1" : "iPod Touch 2G",
"iPod3,1" : "iPod Touch 3G",
"iPod4,1" : "iPod Touch 4G",
"iPod5,1" : "iPod Touch 5G",
"iPod7,1" : "iPod Touch 6G",
"iPad1,1" : "iPad",
"iPad2,1" : "iPad 2 (WiFi)",
"iPad2,2" : "iPad 2 (GSM)",
"iPad2,3" : "iPad 2 (CDMA)",
"iPad2,4" : "iPad 2 (WiFi)",
"iPad2,5" : "iPad Mini (WiFi)",
"iPad2,6" : "iPad Mini (GSM)",
"iPad2,7" : "iPad Mini (CDMA)",
"iPad3,1" : "iPad 3 (WiFi)",
"iPad3,2" : "iPad 3 (CDMA)",
"iPad3,3" : "iPad 3 (GSM)",
"iPad3,4" : "iPad 4 (WiFi)",
"iPad3,5" : "iPad 4 (GSM)",
"iPad3,6" : "iPad 4 (CDMA)",
"iPad4,1" : "iPad Air (WiFi)",
"iPad4,2" : "iPad Air (GSM)",
"iPad4,3" : "iPad Air (CDMA)",
"iPad4,4" : "iPad Mini 2 (WiFi)",
"iPad4,5" : "iPad Mini 2 (Cellular)",
"iPad4,6" : "iPad Mini 2 (Cellular)",
"iPad4,7" : "iPad Mini 3 (WiFi)",
"iPad4,8" : "iPad Mini 3 (Cellular)",
"iPad4,9" : "iPad Mini 3 (Cellular)",
"iPad5,1" : "iPad Mini 4 (WiFi)",
"iPad5,2" : "iPad Mini 4 (Cellular)",
"iPad5,3" : "iPad Air 2 (WiFi)",
"iPad5,4" : "iPad Air 2 (Cellular)",
"iPad6,3" : "iPad Pro 9.7-inch (WiFi)",
"iPad6,4" : "iPad Pro 9.7-inch (Cellular)",
"iPad6,7" : "iPad Pro 12.9-inch (WiFi)",
"iPad6,8" : "iPad Pro 12.9-inch (Cellular)",
"iPad6,11" : "iPad 5 (WiFi)",
"iPad6,12" : "iPad 5 (Cellular)",
"iPad7,1" : "iPad Pro 12.9-inch 2 (WiFi)",
"iPad7,2" : "iPad Pro 12.9-inch 2 (Cellular)",
"iPad7,3" : "iPad Pro 10.5-inch (WiFi)",
"iPad7,4" : "iPad Pro 10.5-inch (Cellular)",
"iPad7,5" : "iPad 6 (WiFi)",
"iPad7,6" : "iPad 6 (Cellular)",
"iPad8,1" : "iPad Pro 11-inch (WiFi)",
"iPad8,2" : "iPad Pro 11-inch (WiFi, 1TB)",
"iPad8,3" : "iPad Pro 11-inch (Cellular)",
"iPad8,4" : "iPad Pro 11-inch (Cellular, 1TB)",
"iPad8,5" : "iPad Pro 12.9-inch 3 (WiFi)",
"iPad8,6" : "iPad Pro 12.9-inch 3 (WiFi, 1TB)",
"iPad8,7" : "iPad Pro 12.9-inch 3 (Cellular)",
"iPad8,8" : "iPad Pro 12.9-inch 3 (Cellular, 1TB)",
"i386" : "Simulator",
"x86_64" : "Simulator",
]
}
当我有了新的watchs平台字符串时,我将更新答案。
+ (NSString *)platformString{
NSString *platform = [Settings platform];
//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)";
// 2018 models
if ([platform isEqualToString:@"iPhone11,1"]) return @"iPhone XR (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone11,2"]) return @"iPhone XS (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone11,3"]) return @"iPhone XS (GSM)";
if ([platform isEqualToString:@"iPhone11,4"]) return @"iPhone XS Max (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone11,5"]) return @"iPhone XS Max (GSM, Dual Sim, China)";
if ([platform isEqualToString:@"iPhone11,6"]) return @"iPhone XS Max (GSM)";
if ([platform isEqualToString:@"iPhone11,8"]) return @"iPhone XR (GSM)";
//iPod Touch
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";
//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\" (Wi-Fi)";
if ([platform isEqualToString:@"iPad6,4"]) return @"iPad Pro 9.7\" (Cellular)";
if ([platform isEqualToString:@"iPad6,7"]) return @"iPad Pro 12.9\" (Wi-Fi)";
if ([platform isEqualToString:@"iPad6,8"]) return @"iPad Pro 12.9\" (Cellular)";
if ([platform isEqualToString:@"iPad6,11"]) return @"iPad (5th generation) (Wi-Fi)";
if ([platform isEqualToString:@"iPad6,12"]) return @"iPad (5th generation) (Cellular)";
if ([platform isEqualToString:@"iPad7,1"]) return @"iPad Pro 12.9\" (2nd generation) (Wi-Fi)";
if ([platform isEqualToString:@"iPad7,2"]) return @"iPad Pro 12.9\" (2nd generation) (Cellular)";
if ([platform isEqualToString:@"iPad7,3"]) return @"iPad Pro 10.5\" (Wi-Fi)";
if ([platform isEqualToString:@"iPad7,4"]) return @"iPad Pro 10.5\" (Cellular)";
if ([platform isEqualToString:@"iPad7,5"]) return @"iPad 6 (WiFi)";
if ([platform isEqualToString:@"iPad7,6"]) return @"iPad 6 (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)";
// 2018 Models
//Simulator
if ([platform isEqualToString:@"i386"]) return @"Simulator";
if ([platform isEqualToString:@"x86_64"]) return @"Simulator";
return platform;
return [@"Unknown : " stringByAppendingString:platform];
}
这是获取设备平台的实际代码。
请注意class methods
,代码位于称为NSObject Class
的{{1}}内部。
您可以使用第一行Settings
和instance method
轻松将其更改为- (NSString *)platform {
,也可以调用平台:- (NSString *)platformString {
在{{1}的顶部}代码。
NSString *platform = [self platform];