Cuxaro Cuxaro - 2 months ago 25
Objective-C Question

Detect Apple Device Model

I need to detect if the device is an iPhone 5 or iPhone 5s.
I've searched the forum but I could not find any solution.

How I can do?

Thanks.

Answer

Use this code in order to get current device model:

#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:@"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;
}