检测特定的iPhone/iPod touch型号

检测特定的iPhone/iPod touch型号,iphone,objective-c,ios,p2p,device,Iphone,Objective C,Ios,P2p,Device,可能重复: 我正在制作一款游戏,它利用了iPhone的点对点蓝牙功能,可能还有第二代iPod touch。然而,为了阻止用户尝试在iPod1st gen和iPhone2G上玩多人游戏,我需要检查具体的设备型号 [[UIDevice currentDevice]model]只会告诉我该设备是iPhone还是iPod touch。有没有办法检查具体的设备型号,比如:iPhone3GS、iPodtouch第一代或其他什么 编辑: UIDevice有一个类别,我认为它是由Erica Sadun创建的,

可能重复:

我正在制作一款游戏,它利用了iPhone的点对点蓝牙功能,可能还有第二代iPod touch。然而,为了阻止用户尝试在iPod1st gen和iPhone2G上玩多人游戏,我需要检查具体的设备型号

[[UIDevice currentDevice]model]只会告诉我该设备是iPhone还是iPod touch。有没有办法检查具体的设备型号,比如:iPhone3GS、iPodtouch第一代或其他什么

编辑:

UIDevice有一个类别,我认为它是由Erica Sadun创建的,我不认为它使用以下代码来获得特定的设备型号。您可以在此处找到整个类别以及其他有用的内容:


这是可行的,使用它的应用程序最近已在AppStore中获得批准。

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

#import <sys/utsname.h>

NSString*
machineName()
{
    struct utsname systemInfo;
    uname(&systemInfo);

    return [NSString stringWithCString:systemInfo.machine
                              encoding:NSUTF8StringEncoding];
}
结果应该是:

@"i386" on the simulator @"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 @"iPhone1,1" on iPhone @"iPhone1,2" on iPhone 3G @"iPhone2,1" on iPhone 3GS @"iPad1,1" on iPad @"iPad2,1" on iPad 2 @"iPad3,1" on iPad 3 (aka new iPad) @"iPhone3,1" on iPhone 4 @"iPhone4,1" on iPhone 4S @"iPhone5,1" on iPhone 5 @"iPhone5,2" on iPhone 5
最完整的UIDevice硬件类别可能由Erica Sadun提供:

[[UIDevice currentDevice] platformType]   // ex: UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // ex: @"iPhone 4G"
然后检查字符串是否等于您要查找的任何设备,如:

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

你应该准备好了

iphone4是iPhone3,1和iPhone3,2 iPhone4S是iPhone4,1 iPad2是iPad2,1 iPad2,2和iPad2,3,具体取决于GSM等版本 iPad3是iPad3,1 iPad3,2和iPad3,3,具体取决于GSM等版本

请参阅向下滚动至内部产品代码

另一个好消息来源是:

这段代码怎么样?如果发布了新版本,您将使用最后知道的设备识别代码

#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;
}

谢谢你的回复。成功了!更正一下:iPhone3GS是iPhone2,1。干杯,谢谢。我已经用iPhone3GS的结果更新了答案。iPhone2Gen的系统信息是什么?它和iPhone 3G一样吗?@x86_64用于模拟器。我不确定是我还是什么,但当我在4、4s和5上测试时,它在开发过程中对我起了作用,但在生产过程中失败了,似乎根本不起作用,这导致了很多问题。这并没有返回准确的型号。只是它有高清或SD屏幕。这对我来说太完美了。我所需要知道的是设备是否有高分辨率,它今天不工作。我在[UIDevice currentDevice]中找不到platformType和platformString。@RobertYiJiang您添加了答案中链接的Github项目中的类吗?它仍然有效!您需要添加include not working a get output as x86_64In>SO问题,一个链接已发布到github.com/ars/uidevice extension/tree/master>此网站。非常重要:苹果建议不要使用此代码。这是因为在某些情况下,这可能会被错误地检测到。在3点30分查看WWDC 2011第123课时,你会看到他们展示了这段代码,并告诉你不要使用。@Lookez我们必须使用什么代码?我看了那个视频。不建议在检查iPad时使用这种代码,因为我们现在已经有了UI_用户_界面_习惯用法,但是没有提到使用它来确定具体的硬件模型。是的,在某些情况下,这并不是可以错误地检测到的,而是总是正确地检测到的。您只需对代码进行未来验证,而不必具有默认为特定版本的某些悬而未决的其他条件。如果有人提到为什么会有-11,但没有评论说明原因,那就太好了。代码[[UIDevice currentDevice]model]只返回iPhone或iPad,而不是型号。
NSString* valueDevice = [[UIDevice currentDevice] model];
if(value==@"iPod1,1" ) 
{}
#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;
}