如何知道设备类型

时间:2013-01-01 07:32:02

标签: iphone objective-c ios

我试图找出一种简单的方法来了解我的Device类型。

我已经知道了

  [[UIDevice currentDevice] systemVersion]

给我一​​般类型。

但我真正需要的是现在它是4,4S,5或任何其他类型的iPhone

4 个答案:

答案 0 :(得分:7)

您可以使用此方法获取您的平台和设备类型

- (NSString *) platformString{
    // Gets a string with the device model
    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); 
    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 2G";
    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";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"iPhone 4 (CDMA)";    
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";

    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch (1 Gen)";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch (2 Gen)";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch (3 Gen)";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch (4 Gen)";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch (5 Gen)";

    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad1,2"])      return @"iPad 3G";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";

    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
    return platform;
}

答案 1 :(得分:1)

您可以使用此类从此下​​方链接中获取设备类型和设备版本。

UIDeviceHardware

只需阅读如何使用以及如何获得平台的说明......

在这里您得到的回应就像示例... Verizon iPhone 4,iPhone 4S,iPod Touch 1G,iPod Touch 2G等。

我希望这对你有帮助......

答案 2 :(得分:1)

我使用当前可用的设备更新了列表。我建议在UIDevice上创建一个类别,让我们称之为UIDevice+Platform,并添加以下两种方法:

- (NSString *)rawPlatform
{
    static NSString *platform = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        size_t size;
        sysctlbyname("hw.machine", NULL, &size, NULL, 0);
        char *machine = malloc(size);
        sysctlbyname("hw.machine", machine, &size, NULL, 0);
        platform = [NSString stringWithUTF8String:machine];
        free(machine);
    });
    return platform;
}

- (NSString *)exactModel
{
    static NSString *platform = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        platform = [self rawPlatform];
        if ([platform isEqualToString:@"iPhone1,1"])
            platform = @"iPhone 1G";
        else if ([platform isEqualToString:@"iPhone1,2"])
            platform = @"iPhone 3G";
        else if ([platform isEqualToString:@"iPhone2,1"])
            platform = @"iPhone 3GS";
        else if ([platform isEqualToString:@"iPhone3,1"])
            platform = @"iPhone 4";
        else if ([platform isEqualToString:@"iPhone3,3"])
            platform = @"Verizon iPhone 4";
        else if ([platform isEqualToString:@"iPhone4,1"])
            platform = @"iPhone 4S";
        else if ([platform isEqualToString:@"iPhone5,1"])
            platform = @"iPhone 5";
        else if ([platform isEqualToString:@"iPhone5,2"])
            platform = @"iPhone 5 (GSM+CDMA)";
        else if ([platform isEqualToString:@"iPhone5,3"])
            platform = @"iPhone 5C";
        else if ([platform isEqualToString:@"iPhone5,4"])
            platform = @"iPhone 5C (GSM+CDMA)";
        else if ([platform isEqualToString:@"iPhone6,1"])
            platform = @"iPhone 5S";
        else if ([platform isEqualToString:@"iPhone6,2"])
            platform = @"iPhone 5S (GSM+CDMA)";
        else if ([platform isEqualToString:@"iPhone7,1"])
            platform = @"iPhone 6+";
        else if ([platform isEqualToString:@"iPhone7,2"])
            platform = @"iPhone 6";
        else if ([platform isEqualToString:@"iPod1,1"])
            platform = @"iPod Touch 1G";
        else if ([platform isEqualToString:@"iPod2,1"])
            platform = @"iPod Touch 2G";
        else if ([platform isEqualToString:@"iPod3,1"])
            platform = @"iPod Touch 3G";
        else if ([platform isEqualToString:@"iPod4,1"])
            platform = @"iPod Touch 4G";
        else if ([platform isEqualToString:@"iPod5,1"])
            platform = @"iPod Touch 5G";
        else if ([platform isEqualToString:@"iPad1,1"])
            platform =  @"iPad 1";
        else if ([platform isEqualToString:@"iPad2,1"])
            platform = @"iPad 2 (WiFi)";
        else if ([platform isEqualToString:@"iPad2,2"])
            platform =  @"iPad 2 (GSM)";
        else if ([platform isEqualToString:@"iPad2,3"])
            platform = @"iPad 2 (CDMA)";
        else if ([platform isEqualToString:@"iPad2,1"])
            platform = @"iPad 2 (WiFi)";
        else if ([platform isEqualToString:@"iPad2,2"])
            platform =  @"iPad 2 (GSM)";
        else if ([platform isEqualToString:@"iPad2,3"])
            platform = @"iPad 2 (CDMA)";
        else if ([platform isEqualToString:@"iPad2,4"])
            platform = @"iPad 2";
        else if ([platform isEqualToString:@"iPad2,5"])
            platform = @"iPad Mini (Wifi)";
        else if ([platform isEqualToString:@"iPad2,6"])
            platform = @"iPad Mini";
        else if ([platform isEqualToString:@"iPad2,7"])
            platform = @"iPad Mini (GSM+CDMA)";
        else if ([platform isEqualToString:@"iPad3,1"])
            platform = @"iPad 3 (WiFi)";
        else if ([platform isEqualToString:@"iPad3,2"])
            platform = @"iPad 3 (GSM+CDMA)";
        else if ([platform isEqualToString:@"iPad3,3"])
            platform = @"iPad 3 (4G,3)";
        else if ([platform isEqualToString:@"iPad3,4"])
            platform = @"iPad 4 (WiFi)";
        else if ([platform isEqualToString:@"iPad3,5"])
            platform = @"iPad 4 (4G,2)";
        else if ([platform isEqualToString:@"iPad3,6"])
            platform = @"iPad 4 (GSM+CDMA)";
        else if ([platform isEqualToString:@"iPad4,1"])
            platform = @"iPad Air (Wifi)";
        else if ([platform isEqualToString:@"iPad4,2"])
            platform = @"iPad Air (GSM+CDMA)";
        else if ([platform isEqualToString:@"iPad4,3"])
            platform = @"iPad Air";
        else if ([platform isEqualToString:@"iPad4,4"])
            platform = @"iPad Mini 2 (GSM)";
        else if ([platform isEqualToString:@"iPad4,5"])
            platform = @"iPad Mini 2 (CDMA)";
        else if ([platform isEqualToString:@"iPad4,6"])
            platform = @"iPad Mini 2 (Wifi)";
        else if ([platform isEqualToString:@"iPad4,7"])
            platform = @"iPad Mini 3 (Wifi)";
        else if ([platform isEqualToString:@"iPad4,8"])
            platform = @"iPad Mini 3 (GSM+CDMA)";
        else if ([platform isEqualToString:@"iPad4,9"])
            platform = @"iPad Mini 3";
        else if ([platform isEqualToString:@"iPad5,3"])
            platform = @"iPad Air 2 (Wifi)";
        else if ([platform isEqualToString:@"iPad5,4"])
            platform = @"iPad Air 2 (GSM+CDMA)";
        else if ([platform isEqualToString:@"i386"])
            platform = @"Simulator";
        else if ([platform isEqualToString:@"x86_64"])
            platform = @"Simulator";
    });

    return platform;
}

答案 3 :(得分:0)

UIDevice类中查找iPhone类型的属性:

@property(nonatomic,readonly,retain) NSString    *systemVersion;     // e.g. @"4.0"