检测特定的iPhone / iPod touch型号[重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检测特定的iPhone / iPod touch型号[重复]相关的知识,希望对你有一定的参考价值。

可能重复: Determine device (iPhone, iPod Touch) with iOS

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

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

编辑:

UIDevice有一个类别(我认为它是由Erica Sadun创建的,我不赞成它),它使用以下代码来获取特定的设备模型。你可以在这里找到整个类别以及其他有用的东西: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"
另一答案

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

#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
另一答案

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

#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等) iPad 3是iPad3,1 iPad3,2和iPad3,3取决于版本(GSM等)

请参阅Iphone secrets(向下滚动到“内部产品代码”)

另一个好消息来源是:everyiphone.com

另一答案
NSString* valueDevice = [[UIDevice currentDevice] model];

然后检查字符串是否等于您要查找的任何设备:

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

你应该好好去

以上是关于检测特定的iPhone / iPod touch型号[重复]的主要内容,如果未能解决你的问题,请参考以下文章

iPhone/iPod Touch检测

检测设备型号(iPhone/iPod Touch)的正确方法?

检测 iPhone/iPad/iPod touch 的颜色?

检测 iPhone/iPad/iPod touch 的颜色?

iPod Touch 和 iPhone 上的核心位置有啥区别?

如何识别设备(iPod touch/iPhone)是 2G 还是 3G?