开发者社区> 问答> 正文

如何判断iOS设备硬件型号

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

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

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

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

展开
收起
a123456678 2016-07-19 16:12:04 2188 0
1 条回答
写回答
取消 提交回答
  • + (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;
    }
    2019-07-17 19:58:30
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载