如何判断iOS设备硬件型号

agassi_yzh
  • 819

如何判断iOS设备型号,比如iPhone5.

现在我的做法是写了个宏:

#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

API中直接有方法可以调用吗?

回复
阅读 20.7k
3 个回答
✓ 已被采纳
+ (NSString*)getMachine{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *name = malloc(size);
    sysctlbyname("hw.machine", name, &size, NULL, 0);
    
    NSString *machine = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
    
    free(name);

    if( [machine isEqualToString:@"i386"] || [machine isEqualToString:@"x86_64"] ) machine = @"ios_Simulator";
    else if( [machine isEqualToString:@"iPhone1,1"] ) machine = @"iPhone_1G";
    else if( [machine isEqualToString:@"iPhone1,2"] ) machine = @"iPhone_3G";
    else if( [machine isEqualToString:@"iPhone2,1"] ) machine = @"iPhone_3GS";
    else if( [machine isEqualToString:@"iPhone3,1"] ) machine = @"iPhone_4";
    else if( [machine isEqualToString:@"iPod1,1"] ) machine = @"iPod_Touch_1G";
    else if( [machine isEqualToString:@"iPod2,1"] ) machine = @"iPod_Touch_2G";
    else if( [machine isEqualToString:@"iPod3,1"] ) machine = @"iPod_Touch_3G";
    else if( [machine isEqualToString:@"iPod4,1"] ) machine = @"iPod_Touch_4G";
    else if( [machine isEqualToString:@"iPad1,1"] ) machine = @"iPad_1";
    else if( [machine isEqualToString:@"iPad2,1"] ) machine = @"iPad_2";
    
    return machine;
}

历史上是这样的

为什么要判断设备型号?如果是为了适应不同分辨率的需求,我是拿屏幕尺寸

[[UIScreen mainScreen] bounds].size

如果是装逼型需求,比如“用户通过iPhone5分享”,试试

char    deviceString[256];
size_t  size = 255;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
if (size > 255) { size = 255; }
sysctlbyname("hw.machine", deviceString, &size, NULL, 0);
if (strcmp(deviceString,"iPhone1,1") == 0) { etc... }  // 2G

iPhone2,1 3GS
iPhone3,1 4
iPhone3,3 4 CDMA
iPhone4,1 4s 猜的
iPhone5,1 5 猜的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏