检测Apple设备型号

时间:2014-03-16 13:01:10

标签: ios objective-c device

我需要检测设备是iPhone 5还是iPhone 5s。 我已经搜索了论坛,但我找不到任何解决方案。

我该怎么做?

感谢。

2 个答案:

答案 0 :(得分:4)

使用此代码以获取当前的设备型号:

#import <sys/utsname.h>

+ (NSString *) deviceModel
{ 
    struct utsname systemInfo;
    uname(&systemInfo);

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

    if ([deviceOriginalName isEqualToString:@"iPhone1,1"])        deviceFormattedName = @"iPhone 2G";                   //iPhone 2G
    else if ([deviceOriginalName isEqualToString:@"iPhone1,2"])   deviceFormattedName = @"iPhone 3G";              //iPhone 3G
    else if ([deviceOriginalName isEqualToString:@"iPhone2,1"])   deviceFormattedName = @"iPhone 3GS";             //iPhone 3GS
    else if ([deviceOriginalName isEqualToString:@"iPhone3,1"])   deviceFormattedName = @"iPhone 4";               //iPhone 4 - AT&T
    else if ([deviceOriginalName isEqualToString:@"iPhone3,2"])   deviceFormattedName = @"iPhone 4";               //iPhone 4 - Other carrier
    else if ([deviceOriginalName isEqualToString:@"iPhone3,3"])   deviceFormattedName = @"iPhone 4";               //iPhone 4 - Other carrier
    else if ([deviceOriginalName isEqualToString:@"iPhone4,1"])   deviceFormattedName = @"iPhone 4S";              //iPhone 4S
    else if ([deviceOriginalName isEqualToString:@"iPhone5,1"])   deviceFormattedName = @"iPhone 5";               //iPhone 5 (GSM)
    else if ([deviceOriginalName isEqualToString:@"iPhone5,2"])   deviceFormattedName = @"iPhone 5";               //iPhone 5 (GSM+CDMA)
    else if ([deviceOriginalName isEqualToString:@"iPhone5,3"])   deviceFormattedName = @"iPhone 5c";              //iPhone 5c (GSM)
    else if ([deviceOriginalName isEqualToString:@"iPhone5,4"])   deviceFormattedName = @"iPhone 5c";              //iPhone 5c (GSM+CDMA)
    else if ([deviceOriginalName isEqualToString:@"iPhone6,1"])   deviceFormattedName = @"iPhone 5s";              //iPhone 5s (GSM)
    else if ([deviceOriginalName isEqualToString:@"iPhone6,2"])   deviceFormattedName = @"iPhone 5s";              //iPhone 5s (GSM+CDMA)
    else if ([deviceOriginalName isEqualToString:@"iPhone7,1"])   deviceFormattedName = @"iPhone 6 Plus";          //iPhone 6+
    else if ([deviceOriginalName isEqualToString:@"iPhone7,2"])   deviceFormattedName = @"iPhone 6";               //iPhone 6
    else if ([deviceOriginalName isEqualToString:@"iPhone8,1"])   deviceFormattedName = @"iPhone 6S";              //iPhone 6S
    else if ([deviceOriginalName isEqualToString:@"iPhone8,2"])   deviceFormattedName = @"iPhone 6S Plus";         //iPhone 6S+
    else if ([deviceOriginalName isEqualToString:@"iPhone8,4"])   deviceFormattedName = @"iPhone SE";              //iPhone SE
    else if ([deviceOriginalName isEqualToString:@"iPhone9,1"])   deviceFormattedName = @"iPhone 7";               //iPhone 7 (CDMA)
    else if ([deviceOriginalName isEqualToString:@"iPhone9,3"])   deviceFormattedName = @"iPhone 7";               //iPhone 7 (GSM)
    else if ([deviceOriginalName isEqualToString:@"iPhone9,2"])   deviceFormattedName = @"iPhone 7 Plus";          //iPhone 7 Plus (CDMA)
    else if ([deviceOriginalName isEqualToString:@"iPhone9,4"])   deviceFormattedName = @"iPhone 7 Plus";          //iPhone 7 Plus (GSM)
    else if ([deviceOriginalName isEqualToString:@"iPhone10,1"])  deviceFormattedName = @"iPhone 8";               //iPhone 8 (CDMA)
    else if ([deviceOriginalName isEqualToString:@"iPhone10,4"])  deviceFormattedName = @"iPhone 8";               //iPhone 8 (GSM)
    else if ([deviceOriginalName isEqualToString:@"iPhone10,2"])  deviceFormattedName = @"iPhone 8 Plus";          //iPhone 8 Plus (CDMA)
    else if ([deviceOriginalName isEqualToString:@"iPhone10,5"])  deviceFormattedName = @"iPhone 8 Plus";          //iPhone 8 Plus (GSM)
    else if ([deviceOriginalName isEqualToString:@"iPhone10,3"])  deviceFormattedName = @"iPhone X";               //iPhone X (CDMA)
    else if ([deviceOriginalName isEqualToString:@"iPhone10,6"])  deviceFormattedName = @"iPhone X";               //iPhone X (GSM)

    else if ([deviceOriginalName isEqualToString:@"iPod1,1"])     deviceFormattedName = @"iPod Touch 1st Gen";     //iPod Touch 1G
    else if ([deviceOriginalName isEqualToString:@"iPod2,1"])     deviceFormattedName = @"iPod Touch 2nd Gen";     //iPod Touch 2G
    else if ([deviceOriginalName isEqualToString:@"iPod3,1"])     deviceFormattedName = @"iPod Touch 3rd Gen";     //iPod Touch 3G
    else if ([deviceOriginalName isEqualToString:@"iPod4,1"])     deviceFormattedName = @"iPod Touch 4th Gen";     //iPod Touch 4G
    else if ([deviceOriginalName isEqualToString:@"iPod5,1"])     deviceFormattedName = @"iPod Touch 5th Gen";     //iPod Touch 5G
    else if ([deviceOriginalName isEqualToString:@"iPod7,1"])     deviceFormattedName = @"iPod Touch 6th Gen";     //iPod Touch 6G

    else if ([deviceOriginalName isEqualToString:@"iPad1,1"])     deviceFormattedName = @"iPad 1";                 //iPad Wifi
    else if ([deviceOriginalName isEqualToString:@"iPad1,2"])     deviceFormattedName = @"iPad 1";                 //iPad 3G
    else if ([deviceOriginalName isEqualToString:@"iPad2,1"])     deviceFormattedName = @"iPad 2";                 //iPad 2 (WiFi)
    else if ([deviceOriginalName isEqualToString:@"iPad2,2"])     deviceFormattedName = @"iPad 2";                 //iPad 2 (GSM)
    else if ([deviceOriginalName isEqualToString:@"iPad2,3"])     deviceFormattedName = @"iPad 2";                 //iPad 2 (CDMA)
    else if ([deviceOriginalName isEqualToString:@"iPad2,4"])     deviceFormattedName = @"iPad 2";                 //iPad 2 (WiFi)
    else if ([deviceOriginalName isEqualToString:@"iPad2,5"])     deviceFormattedName = @"iPad Mini";              //iPad Mini (WiFi)
    else if ([deviceOriginalName isEqualToString:@"iPad2,6"])     deviceFormattedName = @"iPad Mini";              //iPad Mini (GSM)
    else if ([deviceOriginalName isEqualToString:@"iPad2,7"])     deviceFormattedName = @"iPad Mini";              //iPad Mini (GSM+CDMA)
    else if ([deviceOriginalName isEqualToString:@"iPad3,1"])     deviceFormattedName = @"iPad 3";                 //iPad 3 (WiFi)
    else if ([deviceOriginalName isEqualToString:@"iPad3,2"])     deviceFormattedName = @"iPad 3";                 //iPad 3 (GSM+CDMA)
    else if ([deviceOriginalName isEqualToString:@"iPad3,3"])     deviceFormattedName = @"iPad 3";                 //iPad 3 (GSM)
    else if ([deviceOriginalName isEqualToString:@"iPad3,4"])     deviceFormattedName = @"iPad 4";                 //iPad 4 (WiFi)
    else if ([deviceOriginalName isEqualToString:@"iPad3,5"])     deviceFormattedName = @"iPad 4";                 //iPad 4 (GSM)
    else if ([deviceOriginalName isEqualToString:@"iPad3,6"])     deviceFormattedName = @"iPad 4";                 //iPad 4 (GSM+CDMA)
    else if ([deviceOriginalName isEqualToString:@"iPad4,1"])     deviceFormattedName = @"iPad Air";               //iPad Air A1474
    else if ([deviceOriginalName isEqualToString:@"iPad4,2"])     deviceFormattedName = @"iPad Air";               //iPad Air A1475
    else if ([deviceOriginalName isEqualToString:@"iPad4,3"])     deviceFormattedName = @"iPad Air";               //iPad Air A1476
    else if ([deviceOriginalName isEqualToString:@"iPad4,4"])     deviceFormattedName = @"iPad Mini";              //iPad Mini 2 A1489
    else if ([deviceOriginalName isEqualToString:@"iPad4,5"])     deviceFormattedName = @"iPad Mini";              //iPad Mini 2 A1490
    else if ([deviceOriginalName isEqualToString:@"iPad4,6"])     deviceFormattedName = @"iPad Mini";              //iPad Mini 2 A1491
    else if ([deviceOriginalName isEqualToString:@"iPad4,7"])     deviceFormattedName = @"iPad Mini";              //iPad Mini 3 A1599
    else if ([deviceOriginalName isEqualToString:@"iPad4,8"])     deviceFormattedName = @"iPad Mini";              //iPad Mini 3 A1600
    else if ([deviceOriginalName isEqualToString:@"iPad4,9"])     deviceFormattedName = @"iPad Mini";              //iPad Mini 3 A1601
    else if ([deviceOriginalName isEqualToString:@"iPad5,1"])     deviceFormattedName = @"iPad Mini";              //iPad Mini 4 A1538
    else if ([deviceOriginalName isEqualToString:@"iPad5,2"])     deviceFormattedName = @"iPad Mini";              //iPad Mini 4 A1550
    else if ([deviceOriginalName isEqualToString:@"iPad5,3"])     deviceFormattedName = @"iPad Air";               //iPad Air 2 A1566
    else if ([deviceOriginalName isEqualToString:@"iPad5,4"])     deviceFormattedName = @"iPad Air";               //iPad Air 2 A1567
    else if ([deviceOriginalName isEqualToString:@"iPad6,7"])     deviceFormattedName = @"iPad Pro 12.9\"";          //iPad Pro 12.9"
    else if ([deviceOriginalName isEqualToString:@"iPad6,4"])     deviceFormattedName = @"iPad Pro 9.7\"";           //iPad Pro 9.7"

    else if ([deviceOriginalName isEqualToString:@"i386"])        deviceFormattedName = @"Simulator";              //Simulator
    else if ([deviceOriginalName isEqualToString:@"x86_64"])      deviceFormattedName = @"Simulator";              //Simulator

    return deviceFormattedName;
}

答案 1 :(得分:1)

#import <sys/utsname.h>

struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceModel = [NSString stringWithCString:systemInfo.machine   encoding:NSUTF8StringEncoding];

deviceModel 包含设备类型。例如:

适用于iPhone 5的iPhone5,1(型号A1428)

适用于iPhone 5的iPhone5,2(型号A1429)

适用于iPhone 5c的iPhone5,3(型号A1456,A1532)

适用于iPhone 5c的iPhone5,4(型号A1507,A1516,A1526,A1529)

适用于iPhone 5s的iPhone6,1(型号A1433,A1533)

适用于iPhone 5s的iPhone6,2(型号A1457,A1518,A1528,A1530)