以编程方式识别iphone设备

时间:2009-09-10 07:37:46

标签: iphone objective-c ios

我想识别安装我的应用程序的设备。 这意味着,当我在iPhone上安装应用程序时,它应该告诉我设备信息。 信息就像,设备是2G,3G或3GS。

目前我只获得设备的型号,名称,systemName,systemVersion。

我想知道该设备是2G,3G或3GS。

请帮帮我。

3 个答案:

答案 0 :(得分:8)

在过去的Mac编程历史中,这一直被认为是错误的问题。

您真正需要知道的是更具体的信息。为代码中的每个行为决策提出一个更狭隘的问题。例如,您可能需要知道设备是否具有GPS。另一个原因是你不应该根据它的设备来决定如何使用OpenGL堆栈,而应该根据提供的OpenGL功能/扩展信息来决定。

您现在获得的信息来自UIDevice,这是一个不幸的设计不佳的API。它以最糟糕的格式为您提供完全错误的信息 - 字符串。

Erica Sadun对UIDevice的扩展可能很有用here。它只是sysctlbyname("hw.machine", ...)的包装器。每种型号的此属性都不同。

但同样,这通常是错误的问题。

答案 1 :(得分:5)

这是我在我的应用中使用的,效果很好

NSString *deviceType = [UIDevice currentDevice].model;
NSLog(@"DEVICE TYPE %@", deviceType);

struct utsname systemInfo;
uname(&systemInfo);

  - (NSString *) platformString{
NSString *platform =  [NSString stringWithCString:systemInfo.machine
                                     encoding:NSUTF8StringEncoding];    
NSLog(@"type ...%@", platform);


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:@"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:@"iPad1,1"])      return @"iPad";
if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
if ([platform isEqualToString:@"i386"])         return @"Simulator";
return platform;
}

希望有所帮助

答案 2 :(得分:0)

我做了这个,它甚至会显示型号:

struct utsname systemInfo;
    uname(&systemInfo);

    NSString *modelName = [NSString stringWithCString:systemInfo.machine
                                             encoding:NSUTF8StringEncoding];

    NSString *machinename = [NSString stringWithCString:systemInfo.machine
                                               encoding:NSUTF8StringEncoding];


    NSString *modelnumber = @"Not listed";

    //Simulator
    if([modelName isEqualToString:@"i386"] || [modelName isEqualToString:@"x86_64"]) {
        modelName = @"iPhone Simulator";
        modelnumber = @"XXXXX";

    }

    //iPhone
    else if([modelName isEqualToString:@"iPhone1,1"]) {
        modelName = @"iPhone";
        modelnumber = @"A1203";
    }
    else if([modelName isEqualToString:@"iPhone1,2"]) {
        modelName = @"iPhone 3G";
        modelnumber = @"A1241/A1324";
    }
    else if([modelName isEqualToString:@"iPhone2,1"]) {
        modelName = @"iPhone 3GS";
        modelnumber = @"A1303/A1325";
    }
    else if([modelName isEqualToString:@"iPhone3,1"]) {
        modelName = @"iPhone 4 (GSM)";
        modelnumber = @"A1332";
    }
    else if([modelName isEqualToString:@"iPhone3,2"]) {
        modelName = @"iPhone 4 GSM Rev A";
        modelnumber = @"Not listed";
    }
    else if([modelName isEqualToString:@"iPhone3,3"]) {
        modelName = @"iPhone 4 (CDMA)";
        modelnumber = @"A1349";
    }
    else if([modelName isEqualToString:@"iPhone4,1"]) {
        modelName = @"iPhone 4S";
        modelnumber = @"A1387/A1431";
    }
    else if([modelName isEqualToString:@"iPhone5,1"]) {
        modelName = @"iPhone 5 (GSM)";
        modelnumber = @"A1428";
    }
    else if([modelName isEqualToString:@"iPhone5,2"]) {
        modelName = @"iPhone 5 (GSM+CDMA)";
        modelnumber = @"A1429/A1442";
    }

    //iPod touch
    else if([modelName isEqualToString:@"iPod1,1"]) {
        modelName = @"iPod touch 1G";
        modelnumber = @"A1213";
    }
    else if([modelName isEqualToString:@"iPod2,1"]) {
        modelName = @"iPod touch 2G";
        modelnumber = @"A1288";
    }
    else if([modelName isEqualToString:@"iPod3,1"]) {
        modelName = @"iPod touch 3G";
        modelnumber = @"A1318";
    }
    else if([modelName isEqualToString:@"iPod4,1"]) {
        modelName = @"iPod touch 4G";
        modelnumber = @"A1367";
    }
    else if([modelName isEqualToString:@"iPod5,1"]) {
        modelName = @"iPod touch 5G";
        modelnumber = @"A1421";
    }

    //iPad
    else if([modelName isEqualToString:@"iPad1,1"]) {
        modelName = @"iPad 1G";
        modelnumber = @"A1219/A1337";
    }
    else if([modelName isEqualToString:@"iPad2,1"]) {
        modelName = @"iPad 2 (WiFi)";
        modelnumber = @"A1395";
    }
    else if([modelName isEqualToString:@"iPad2,2"]) {
        modelName = @"iPad 2 (GSM)";
        modelnumber = @"A1396";
    }
    else if([modelName isEqualToString:@"iPad2,3"]) {
        modelName = @"iPad 2 (CDMA)";
        modelnumber = @"A1397";
    }
    else if([modelName isEqualToString:@"iPad2,4"]) {
        modelName = @"iPad 2 (WiFi + Rev A)";
        modelnumber = @"A1395";
    }
    else if([modelName isEqualToString:@"iPad3,1"]) {
        modelName = @"iPad 3 (WiFi)";
        modelnumber = @"A1416";
    }
    else if([modelName isEqualToString:@"iPad3,2"]) {
        modelName = @"iPad 3 (GSM+CDMA)";
        modelnumber = @"A1403";
    }
    else if([modelName isEqualToString:@"iPad3,3"]) {
        modelName = @"iPad 3 (GSM)";
        modelnumber = @"A1430";
    }
    else if([modelName isEqualToString:@"iPad3,4"]) {
        modelName = @"iPad 4 (WiFi)";
        modelnumber = @"A1458";
    }
    else if([modelName isEqualToString:@"iPad3,5"]) {
        modelName = @"iPad 4 (GSM)";
        modelnumber = @"A1459";
    }
    else if([modelName isEqualToString:@"iPad3,6"]) {
        modelName = @"iPad 4 (GSM+CDMA)";
        modelnumber = @"A1460";
    }

    //iPad mini
    else if([modelName isEqualToString:@"iPad2,5"]) {
        modelName = @"iPad mini (WiFi)";
        modelnumber = @"A1432";
    }
    else if([modelName isEqualToString:@"iPad2,6"]) {
        modelName = @"iPad mini (GSM)";
        modelnumber = @"A1454";
    }
    else if([modelName isEqualToString:@"iPad2,7"]) {
        modelName = @"iPad mini (GSM+CDMA)";
        modelnumber = @"A1455";
    }

    //Apple TV, just for funn:)
    else if([modelName isEqualToString:@"AppleTV2,1"]) {
        modelName = @"Apple TV 2G";
        modelnumber = @"A1378";
    }
    else if([modelName isEqualToString:@"AppleTV3,1"]) {
        modelName = @"Apple TV 3G";
        modelnumber = @"A1427";
    }
    else if([modelName isEqualToString:@"AppleTV3,2"]) {
        modelName = @"Apple TV 3G (Rev A)";
        modelnumber = @"A1469";
    }

    NSLog(@"Device model: %@/ %@ /%@",modelName,machinename,modelnumber);