检测特定的iPhone / iPod touch型号

可能重复:
用iPhone SDK确定设备(iPhone,iPod Touch)

我正在制作一款利用iPhone的点对点蓝牙function(可能是iPod touch第二代)的游戏。 但是,要阻止用户尝试在iPod 1st Gen和iPhone 2G上玩多人游戏,我需要检查特定的设备型号。

[[UIDevice currentDevice]模式]只会告诉我该设备是“iPhone”还是“iPod touch”。 有没有办法检查具体的设备型号,如:“iPhone 3GS”,“iPod touch第一代”或什么的。

编辑:

有一个UIDevice的类别(我认为它是由Erica Sadun创build的,我不赞赏它)使用下面的代码来获取特定的设备模型。 您可以在这里find整个类别以及其他有用的东西: https : //github.com/erica/uidevice-extension

#include <sys/types.h> #include <sys/sysctl.h> @implementation UIDevice (Hardware) /* Platforms iPhone1,1 -> iPhone 1G iPhone1,2 -> iPhone 3G iPod1,1 -> iPod touch 1G iPod2,1 -> iPod touch 2G */ - (NSString *) platform { 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); return platform; } 

这个工程和应用程序使用这个已经在AppStore最近批准。

最完整的UIDevice(硬件)类别可能是http://github.com/erica/uidevice-extension/ (由Erica Sadun提供):

 [[UIDevice currentDevice] platformType] // ex: UIDevice4GiPhone [[UIDevice currentDevice] platformString] // ex: @"iPhone 4G" 

您可以从sys/utsname.h使用uname获取设备型号。 例如:

 #import <sys/utsname.h> NSString* machineName() { struct utsname systemInfo; uname(&systemInfo); return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; } 

结果应该是:

 @模拟器上的“i386”
 @ iPod Touch上的“iPod1,1”
 @ iPod Touch第二代的“iPod2,1”
 @第三代iPod Touch上的“iPod3,1”
 @第四代iPod Touch上的“iPod4,1”
 @ iPhone上的“iPhone1,1”
 @ iPhone 3G上的“iPhone1,2”
 @ iPhone 3GS上的“iPhone2,1”
 @ iPad上的“iPad1,1”
 @ iPad 2上的“iPad2,1”
 iPad 3上的“iPad3,1”(又名新iPad)
 @ iPhone 4上的“iPhone3,1”
 @ iPhone 4S上的“iPhone4,1”
 @ iPhone 5上的“iPhone5,1”
 iPhone 5上的“iPhone5,2”

如果这个代码,如果新版本发布,你将标识与最后一个知道的设备

 #include <sys/types.h> #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 *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 @"iPhone4 AT&T"; //iPhone 4 - AT&T if ([sDeviceModel isEqual:@"iPhone3,2"]) return @"iPhone4 Other"; //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:@"iPhone5,1"]) return @"iPhone5"; //iPhone 5 (GSM) 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; } 
 BOOL hasHighResScreen = NO; if ([UIScreen instancesRespondToSelector:@selector(scale)]) { CGFloat scale = [[UIScreen mainScreen] scale]; if (scale > 1.0) { hasHighResScreen = YES; } } 

iPhone 4是iPhone3,1和iPhone3,2
iPhone 4S是iPhone4,1
iPad 2是iPad2,1 iPad2,2和iPad2,3根据版本(GSM等)
根据版本(GSM等),iPad 3是iPad3,1 iPad3,2和iPad3,3

查看iPhone的机密 (向下滚动到“内部产品代码”)

另一个好的来源是: everyiphone.com

 NSString* valueDevice = [[UIDevice currentDevice] model]; 

然后检查string是否等于您正在寻找的任何设备:

 if(value==@"iPod1,1" ) {} 

你应该很好走