我使用此方法获取设备型号名称:
+ (NSString *) machineModel
{
const char *name = "hw.model";
#if TARGET_OS_IPHONE
// .model on iOS returns internal model name (e.g. "N51AP") when we'd prefer to see the model identifier here ("iPhone6,1")
name = "hw.machine";
#endif
int error = 0;
size_t length = 0;
error = sysctlbyname(name, NULL, &length, NULL, 0);
if (error != 0) {
return nil;
}
char *p = malloc(sizeof(char) * length);
if (p) {
error = sysctlbyname(name, p, &length, NULL, 0);
}
if (error != 0) {
free(p);
return nil;
}
NSString *machinemodel = [NSString stringWithFormat:@"%s", p];
free(p);
return machinemodel;
}
但是,当我在数千台设备上运行此代码时,我在数据库中获得以下选项:
"iPhone7;1"
"iPhone8,1"
"iPhone7;2"
"iPhone"
"iPhone9;3"
"iPhone7;1"
"iPhone8,1"
"iPhone8,1"
"iPhone7,2"
"iPhone7;2"
"iPhone9;1"
"iPhone7;2"
...
有时候我有,
分隔符,有时;
,有时我看不到号码:"iPhone"
为什么会这样?
我希望这可以从Android设备(5K不同的制造商),但为什么iOS只有Apple是所有者
答案 0 :(得分:2)
我相信您通过直接访问模型名称来解决代码问题。可能在某些iOS版本或iOS架构上,它的工作方式不同。
我建议您使用一些更高级别的库,例如DeviceKit或Device - 它对我来说效果非常好,并且始终能够提供正确的结果。
如果你不想直接使用它们,至少你可以看到它们如何工作并自己实现相同的逻辑。
答案 1 :(得分:2)
@snaggs你可以添加这个方法,它会给你正确的输出。 我在第一行方法中调用了你的方法。
- (NSString *)platformString
{
NSString *platform = [self machineModel];
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 (GSM)";
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 (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 GSM";
if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6 GSM+CDMA";
if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6+ GSM";
if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6+ Global";
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 (GSM)";
if ([platform isEqualToString:@"iPad4,4"]) return @"iPad Mini Retina (WiFi)";
if ([platform isEqualToString:@"iPad4,5"]) return @"iPad Mini Retina (GSM)";
if ([platform isEqualToString:@"i386"]) return @"Simulator";
if ([platform isEqualToString:@"x86_64"]) return @"Simulator";
return platform;
}
答案 2 :(得分:0)
有了这些知识,我们终于可以获得“硬件模型”的价值了 使用sysctl获取OS X的HW_MODEL和iOS的HW_MACHINE 系统
在iOS中,您应该使用hw.machine
而不是hw.model
size_t size;
int nR = sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = (char *)malloc(size);
nR = sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *deviceString = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);