1. 程式人生 > >[iOS] How to determine iOS model

[iOS] How to determine iOS model

iOS device 取的到硬體資訊如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CommandUUID</key>
	<string>9cf1dd6f-077d-4171-bda3-6c5dbda572e5</string>
	<key>QueryResponses
</key> <dict> <key>AvailableDeviceCapacity</key> <real>10.320999145507812</real> <key>BatteryLevel</key> <real>0.49000000953674316</real> <key>BluetoothMAC</key> <string>94:94:94:94:94:a6</string> <key>BuildVersion</key> <string>14G60</string> <key>CellularTechnology</key> <integer>3</integer> <key>DataRoamingEnabled</key> <false/> <key>DeviceCapacity</key> <real>12.137939453125</real> <key>DeviceName</key> <string>XXX 的 iPad</string> <key>IMEI</key> <string>99 000289 244779 6</string> <key>IsRoaming</key> <true/> <key>MEID</key> <string>99000289244779</string> <key>Model</key> <string>MD525TA</string> <key>ModelName</key> <string>iPad</string> <key>ModemFirmwareVersion</key> <string>11.60.00</string> <key>OSVersion</key> <string>10.3.3</string> <key>ProductName</key> <string>iPad3,6</string> <key>SerialNumber</key> <string>DMPK3ML3F190</string> <key>UDID</key> <string>38c2840ff0be3ccff43299d2e06320485facb2f5</string> <key>WiFiMAC</key> <string>94:94:94:94:94:a5</string> </dict> <key>Status
</key> <string>Acknowledged</string> <key>UDID</key> <string>38c2840ff0be3ccff43299d2e06320485facb2f5</string> </dict> </plist>

轉換 ProductName 的 code:

// 獲取裝置型號然後手動轉化為對應名稱
- (NSString *)getDeviceName
{
    // 需要#import "sys/utsname.h"
    #warning 題主嘔心瀝血總結!!最全面!親測!全網獨此一份!!
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"iPhone3,2"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"iPhone3,3"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([deviceString isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
    if ([deviceString isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
    if ([deviceString isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([deviceString isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    if ([deviceString isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    if ([deviceString isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    if ([deviceString isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
    // 日行兩款手機型號均為日本獨佔,可能使用索尼FeliCa支付方案而不是蘋果支付
    if ([deviceString isEqualToString:@"iPhone9,1"])    return @"國行、日版、港行iPhone 7";
    if ([deviceString isEqualToString:@"iPhone9,2"])    return @"港行、國行iPhone 7 Plus";
    if ([deviceString isEqualToString:@"iPhone9,3"])    return @"美版、臺版iPhone 7";
    if ([deviceString isEqualToString:@"iPhone9,4"])    return @"美版、臺版iPhone 7 Plus";
    if ([machineString isEqualToString:@"iPhone10,1"])   return @"iPhone_8";
    if ([machineString isEqualToString:@"iPhone10,4"])   return @"iPhone_8";
    if ([machineString isEqualToString:@"iPhone10,2"])   return @"iPhone_8_Plus";
    if ([machineString isEqualToString:@"iPhone10,5"])   return @"iPhone_8_Plus";
    if ([machineString isEqualToString:@"iPhone10,3"])   return @"iPhone_X";
    if ([machineString isEqualToString:@"iPhone10,6"])   return @"iPhone_X";
    
    if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([deviceString isEqualToString:@"iPod5,1"])      return @"iPod Touch (5 Gen)";
    
    if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([deviceString isEqualToString:@"iPad1,2"])      return @"iPad 3G";
    if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2";
    if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([deviceString isEqualToString:@"iPad2,4"])      return @"iPad 2";
    if ([deviceString isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([deviceString isEqualToString:@"iPad2,6"])      return @"iPad Mini";
    if ([deviceString isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([deviceString isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPad3,3"])      return @"iPad 3";
    if ([deviceString isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([deviceString isEqualToString:@"iPad3,5"])      return @"iPad 4";
    if ([deviceString isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
    if ([deviceString isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
    if ([deviceString isEqualToString:@"iPad4,4"])      return @"iPad Mini 2 (WiFi)";
    if ([deviceString isEqualToString:@"iPad4,5"])      return @"iPad Mini 2 (Cellular)";
    if ([deviceString isEqualToString:@"iPad4,6"])      return @"iPad Mini 2";
    if ([deviceString isEqualToString:@"iPad4,7"])      return @"iPad Mini 3";
    if ([deviceString isEqualToString:@"iPad4,8"])      return @"iPad Mini 3";
    if ([deviceString isEqualToString:@"iPad4,9"])      return @"iPad Mini 3";
    if ([deviceString isEqualToString:@"iPad5,1"])      return @"iPad Mini 4 (WiFi)";
    if ([deviceString isEqualToString:@"iPad5,2"])      return @"iPad Mini 4 (LTE)";
    if ([deviceString isEqualToString:@"iPad5,3"])      return @"iPad Air 2";
    if ([deviceString isEqualToString:@"iPad5,4"])      return @"iPad Air 2";
    if ([deviceString isEqualToString:@"iPad6,3"])      return @"iPad Pro 9.7";
    if ([deviceString isEqualToString:@"iPad6,4"])      return @"iPad Pro 9.7";
    if ([deviceString isEqualToString:@"iPad6,7"])      return @"iPad Pro 12.9";
    if ([deviceString isEqualToString:@"iPad6,8"])      return @"iPad Pro 12.9";
    
   if ([deviceString isEqualToString:@"AppleTV2,1"])      return @"Apple TV 2";
   if ([deviceString isEqualToString:@"AppleTV3,1"])      return @"Apple TV 3";
   if ([deviceString isEqualToString:@"AppleTV3,2"])      return @"Apple TV 3";
   if ([deviceString isEqualToString:@"AppleTV5,3"])      return @"Apple TV 4";

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

enum iosPlatform getIosPlatform()
{
static enum iosPlatform eDev = iDeviceUnknown;
#if defined(TARGET_DARWIN_IOS)
if (eDev == iDeviceUnknown)
{
std::string devStr(CDarwinUtils::getIosPlatformString());

if (devStr == “iPhone1,1”) eDev = iPhone2G;
else if (devStr == “iPhone1,2”) eDev = iPhone3G;
else if (devStr == “iPhone2,1”) eDev = iPhone3GS;
else if (devStr == “iPhone3,1”) eDev = iPhone4;
else if (devStr == “iPhone3,2”) eDev = iPhone4;
else if (devStr == “iPhone3,3”) eDev = iPhone4CDMA;
else if (devStr == “iPhone4,1”) eDev = iPhone4S;
else if (devStr == “iPhone5,1”) eDev = iPhone5;
else if (devStr == “iPhone5,2”) eDev = iPhone5GSMCDMA;
else if (devStr == “iPhone5,3”) eDev = iPhone5CGSM;
else if (devStr == “iPhone5,4”) eDev = iPhone5CGlobal;
else if (devStr == “iPhone6,1”) eDev = iPhone5SGSM;
else if (devStr == “iPhone6,2”) eDev = iPhone5SGlobal;
else if (devStr == “iPhone7,1”) eDev = iPhone6Plus;
else if (devStr == “iPhone7,2”) eDev = iPhone6;
else if (devStr == “iPhone8,1”) eDev = iPhone6s;
else if (devStr == “iPhone8,2”) eDev = iPhone6sPlus;
else if (devStr == “iPhone8,4”) eDev = iPhoneSE;
else if (devStr == “iPhone9,1”) eDev = iPhone7;
else if (devStr == “iPhone9,2”) eDev = iPhone7Plus;
else if (devStr == “iPhone9,3”) eDev = iPhone7;
else if (devStr == “iPhone9,4”) eDev = iPhone7Plus;
else if (devStr == “iPhone10,1”) eDev = iPhone8;
else if (devStr == “iPhone10,2”) eDev = iPhone8Plus;
else if (devStr == “iPhone10,3”) eDev = iPhoneX;
else if (devStr == “iPhone10,4”) eDev = iPhone8;
else if (devStr == “iPhone10,5”) eDev = iPhone8Plus;
else if (devStr == “iPhone10,6”) eDev = iPhoneX;
else if (devStr == “iPod1,1”) eDev = iPodTouch1G;
else if (devStr == “iPod2,1”) eDev = iPodTouch2G;
else if (devStr == “iPod3,1”) eDev = iPodTouch3G;
else if (devStr == “iPod4,1”) eDev = iPodTouch4G;
else if (devStr == “iPod5,1”) eDev = iPodTouch5G;
else if (devStr == “iPod7,1”) eDev = iPodTouch6G;
else if (devStr == “iPad1,1”) eDev = iPad;
else if (devStr == “iPad1,2”) eDev = iPad;
else if (devStr == “iPad2,1”) eDev = iPad2WIFI;
else if (devStr == “iPad2,2”) eDev = iPad2;
else if (devStr == “iPad2,3”) eDev = iPad2CDMA;
else if (devStr == “iPad2,4”) eDev = iPad2;
else if (devStr == “iPad2,5”) eDev = iPadMiniWIFI;
else if (devStr == “iPad2,6”) eDev = iPadMini;
else if (devStr == “iPad2,7”) eDev = iPadMiniGSMCDMA;
else if (devStr == “iPad3,1”) eDev = iPad3WIFI;
else if (devStr == “iPad3,2”) eDev = iPad3GSMCDMA;
else if (devStr == “iPad3,3”) eDev = iPad3;
else if (devStr == “iPad3,4”) eDev = iPad4WIFI;
else if (devStr == “iPad3,5”) eDev = iPad4;
else if (devStr == “iPad3,6”) eDev = iPad4GSMCDMA;
else if (devStr == “iPad4,1”) eDev = iPadAirWifi;
else if (devStr == “iPad4,2”) eDev = iPadAirCellular;
else if (devStr == “iPad4,3”) eDev = iPadAirTDLTE;
else if (devStr == “iPad4,4”) eDev = iPadMini2Wifi;
else if (devStr == “iPad4,5”) eDev = iPadMini2Cellular;
else if (devStr == “iPad4,6”) eDev = iPadMini2Cellular;
else if (devStr == “iPad4,7”) eDev = iPadMini3Wifi;
else if (devStr == “iPad4,8”) eDev = iPadMini3Cellular;
else if (devStr == “iPad4,9”) eDev = iPadMini3Cellular;
else if (devStr == “iPad5,1”) eDev = iPadMini4Wifi;
else if (devStr == “iPad5,2”) eDev = iPadMini4Cellular;
else if (devStr == “iPad5,3”) eDev = iPadAir2Wifi;
else if (devStr == “iPad5,4”) eDev = iPadAir2Cellular;
else if (devStr == “iPad6,3”) eDev = iPadPro9_7InchWifi;
else if (devStr == “iPad6,4”) eDev = iPadPro9_7InchCellular;
else if (devStr == “iPad6,7”) eDev = iPadPro12_9InchWifi;
else if (devStr == “iPad6,8”) eDev = iPadPro12_9InchCellular;
else if (devStr == “iPad6,11”) eDev = iPad5Wifi;
else if (devStr == “iPad6,12”) eDev = iPad5Cellular;
else if (devStr == “iPad7,1”) eDev = iPadPro2_12_9InchWifi;
else if (devStr == “iPad7,2”) eDev = iPadPro2_12_9InchCellular;
else if (devStr == “iPad7,3”) eDev = iPadPro_10_5InchWifi;
else if (devStr == “iPad7,4”) eDev = iPadPro_10_5InchCellular;
else if (devStr == “AppleTV2,1”) eDev = AppleTV2;
else if (devStr == “AppleTV5,3”) eDev = AppleTV4;
else if (devStr == “AppleTV6,2”) eDev = AppleTV4K;
}
#endif
return eDev;
}

(Sample Code):

[[UIDevice currentDevice] platformType]   // ex: UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // ex: @"iPhone 4G"

OR You can use this method:

You can get the device model number using uname from sys/utsname.h. For example:

Objective-C

#import <sys/utsname.h> // import it in your header or implementation file.

NSString* deviceName()
{
    struct utsname systemInfo;
    uname(&systemInfo);

    return [NSString stringWithCString:systemInfo.machine
                              encoding:NSUTF8StringEncoding];
}

Swift 3

extension UIDevice {
    var modelName: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        return identifier
    }
}

print(UIDevice.current.modelName)

The result should be:

//Simultor
@"i386"      on 32-bit Simulator
@"x86_64"    on 64-bit Simulator

//iPhone
@"iPhone1,1" on iPhone
@"iPhone1,2" on iPhone 3G
@"iPhone2,1" on iPhone 3GS
@"iPhone3,1" on iPhone 4 (GSM)
@"iPhone3,3" on iPhone 4 (CDMA/Verizon/Sprint)
@"iPhone4,1" on iPhone 4S
@"iPhone5,1" on iPhone 5 (model A1428, AT&T/Canada)
@"iPhone5,2" on iPhone 5 (model A1429, everything else)
@"iPhone5,3" on iPhone 5c (model A1456, A1532 | GSM)
@"iPhone5,4" on iPhone 5c (model A1507, A1516, A1526 (China), A1529 | Global)
@"iPhone6,1" on iPhone 5s (model A1433, A1533 | GSM)
@"iPhone6,2" on iPhone 5s (model A1457, A1518, A1528 (China), A1530 | Global)
@"iPhone7,1" on iPhone 6 Plus
@"iPhone7,2" on iPhone 6
@"iPhone8,1" on iPhone 6S
@"iPhone8,2" on iPhone 6S Plus
@"iPhone8,4" on iPhone SE
@"iPhone9,1" on iPhone 7 (CDMA)
@"iPhone9,3" on iPhone 7 (GSM)
@"iPhone9,2" on iPhone 7 Plus (CDMA)
@"iPhone9,4" on iPhone 7 Plus (GSM)
@"iPhone10,1" on iPhone 8 (CDMA)
@"iPhone10,4" on iPhone 8 (GSM)
@"iPhone10,2" on iPhone 8 Plus (CDMA)
@"iPhone10,5" on iPhone 8 Plus (GSM)
@"iPhone10,3" on iPhone X (CDMA)
@"iPhone10,6" on iPhone X (GSM)

//iPad 1
@"iPad1,1" on iPad - Wifi (model A1219)
@"iPad1,1" on iPad - Wifi + Cellular (model A1337)

//iPad 2
@"iPad2,1" - Wifi (model A1395)
@"iPad2,2" - GSM (model A1396)
@"iPad2,3" - 3G (model A1397)
@"iPad2,4" - Wifi (model A1395)

// iPad Mini
@"iPad2,5" - Wifi (model A1432)
@"iPad2,6" - Wifi + Cellular (model  A1454)
@"iPad2,7" - Wifi + Cellular (model  A1455)

//iPad 3
@"iPad3,1" - Wifi (model A1416)
@"iPad3,2" - Wifi + Cellular (model  A1403)
@"iPad3,3" - Wifi + Cellular (model  A1430)

//iPad 4
@"iPad3,4" - Wifi (model A1458)
@"iPad3,5" - Wifi + Cellular (model  A1459)
@"iPad3,6" - Wifi + Cellular (model  A1460)

//iPad AIR
@"iPad4,1" - Wifi (model A1474)
@"iPad4,2" - Wifi + Cellular (model A1475)
@"iPad4,3" - Wifi + Cellular (model A1476)

// iPad Mini 2
@"iPad4,4" - Wifi (model A1489)
@"iPad4,5" - Wifi + Cellular (model A1490)
@"iPad4,6" - Wifi + Cellular (model A1491)

// iPad Mini 3
@"iPad4,7" - Wifi (model A1599)
@"iPad4,8" - Wifi + Cellular (model A1600)
@"iPad4,9" - Wifi + Cellular (model A1601)

// iPad Mini 4
@"iPad5,1" - Wifi (model A1538)
@"iPad5,2" - Wifi + Cellular (model A1550)

//iPad AIR 2
@"iPad5,3" - Wifi (model A1566)
@"iPad5,4" - Wifi + Cellular (model A1567)

// iPad PRO 12.9"
@"iPad6,3" - Wifi (model A1673)
@"iPad6,4" - Wifi + Cellular (model A1674)
@"iPad6,4" - Wifi + Cellular (model A1675)

//iPad PRO 9.7"
@"iPad6,7" - Wifi (model A1584)
@"iPad6,8" - Wifi + Cellular (model A1652)

//iPod Touch
@"iPod1,1"   on iPod Touch
@"iPod2,1"   on iPod Touch Second Generation
@"iPod3,1"   on iPod Touch Third Generation
@"iPod4,1"   on iPod Touch Fourth Generation
@"iPod7,1"   on iPod Touch 6th Generation