我怎样才能获得iphone设备类型?

时间:2011-10-01 12:34:28

标签: objective-c xcode4 cocos2d-iphone device

我想获得iphone设备类型,如果它是iphone 2或3G或4 xcode 4.0。

有没有办法得到它?

如果是,请告诉我。

8 个答案:

答案 0 :(得分:15)

我找到了一些新算法来获取设备类型和iOS版本。

#include <sys/sysctl.h>
#include <sys/utsname.h>


- (NSString *) platformString{

NSLog(@"[UIDevice currentDevice].model: %@",[UIDevice currentDevice].model);
NSLog(@"[UIDevice currentDevice].description: %@",[UIDevice currentDevice].description);
NSLog(@"[UIDevice currentDevice].localizedModel: %@",[UIDevice currentDevice].localizedModel);
NSLog(@"[UIDevice currentDevice].name: %@",[UIDevice currentDevice].name);
NSLog(@"[UIDevice currentDevice].systemVersion: %@",[UIDevice currentDevice].systemVersion);
NSLog(@"[UIDevice currentDevice].systemName: %@",[UIDevice currentDevice].systemName);
NSLog(@"[UIDevice currentDevice].batteryLevel: %f",[UIDevice currentDevice].batteryLevel);      
struct utsname systemInfo;
uname(&systemInfo);
NSLog(@"[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]: %@",[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]);
NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
if ([platform isEqualToString:@"iPhone3,2"])    return @"iPhone 4 CDMA";
if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (Cellular)";
if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (Cellular)";
if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (Cellular)";
if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (Cellular)";
if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (Cellular)";
if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (Cellular)";
if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (Cellular)";
if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (Cellular)";
if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
if ([platform isEqualToString:@"i386"])         return @"Simulator";
if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
return @"Unknown";
}

答案 1 :(得分:10)

实际上,UIDevice类没有方法platformString。使用未记录的方法,您的应用程序将被Apple拒绝。

[[UIDevice currentDevice] model] // e.g. "iPod touch"

会做到这一点。

答案 2 :(得分:8)

Caleb是对的,你不应该检查设备类型,而是检查功能。例如,您可以检查设备是否支持多任务处理,如下所示:

UIDevice *device = [UIDevice currentDevice];
if ([device respondsToSelector:@selector(isMultitaskingSupported)] && [device isMultitaskingSupported]) {
    // ...code to be executed if multitasking is supported.
    // respondsToSelector: is very useful
}

如果必须,您可以检查iOS版本,但知道这不能代替检查对象respondsToSelector:

#define IOS4_0 40000

// You'd probably want to put this in a convenient method
NSArray *versions = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
NSInteger major = [[versions objectAtIndex:0] intValue];
NSInteger minor = [[versions objectAtIndex:1] intValue];
NSInteger version = major * 10000 + minor * 100;

if (version >= IOS4_0) {
    // ...code to be executed for iOS4.0+
}

据我所知,没有记录的方法来检查设备型号。

答案 3 :(得分:4)

我在我的项目中添加了静态方法来检查设备类型:iPad,iPhone4(或更少)和iPhone5以处理不同的屏幕尺寸。

+ (DeviceType) currentDeviceType
{
    DeviceType device = DEVICE_TYPE_IPAD ;
    if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
    {
        if( [[UIScreen mainScreen] bounds].size.height >= 568 || [[UIScreen mainScreen] bounds].size.width >= 568 )
        {
            device = DEVICE_TYPE_IPHONE5 ;
        }
        else
        {
            device = DEVICE_TYPE_IPHONE4 ;
        }
    }

    return device ;
}

您也可以使用UIUserInterfaceIdiomPad类型。

据我所知,iPod和iPhone的目的是平等对待。但如果您需要确定它是否是iPod,请检查它是否可以拨打电话。

答案 4 :(得分:2)

查看https://github.com/erica/uidevice-extension/

[[UIDevice currentDevice] platformString] //@"iPhone 4"

答案 5 :(得分:2)

This post帮助我区分iPhone / iPad类型:

您可以获得如上所示的型号类型(iPhone,iPad,iPod):

[[UIDevice currentDevice] model]

要进一步指定,您可以通过以下代码导入并查找作为char数组返回的特定模型类型:

struct utsname u;
uname(&u);
char *type = u.machine;

并通过以下方式将其转换为NSString:

NSString *strType = [NSString stringWithFormat:@"%s", type];

答案 6 :(得分:1)

通常的建议是不要担心您正在运行什么类型的设备,而是要测试您需要的功能。如果你看一下设备类型,你肯定会做出不正确的假设。例如,在不同市场销售的每种型号的版本可能会有所不同,并且可能会出现报告相同类型但具有不同功能的新设备。因此,测试功能而不是模型。

答案 7 :(得分:1)

你可以定义一些扩展@csb对这个帖子的回答的宏。


#define IS_IPHONE4 ([[UIScreen mainScreen] bounds].size.width == 480 || [[UIScreen mainScreen] bounds].size.height == 480)
#define IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.width == 568 || [[UIScreen mainScreen] bounds].size.height == 568)
#define IS_IPAD    ([[UIScreen mainScreen] bounds].size.width == 768 || [[UIScreen mainScreen] bounds].size.height == 768)