How to get device make and model on iOS?
I was wondering if it's possible to determine what kind of iPhone (for example) the currentdevice is? I know it's possible to get the model throughNSString *deviceType = [[UIDevice currentDevice] model];
which will just return whether I have an "iPhone" or an "iPod", BUT I was wondering if it's possible to detect/know if I have an iPhone 3GS vs. and iPhone 4 vs. an iPhone 4S (in actuality, all I really want to do is determine if I have a 3G or not, because I'm doing fairly graphics intensive stuff).
So yeah, let me know, thank you!
Solution 1:
EITHER try this library: http://github.com/erica/uidevice-extension/ (by Erica Sadun). (The library is 7-8 years old, and hence is obsolete)
(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:
// Output on a simulator
@"i386" on 32-bit Simulator
@"x86_64" on 64-bit Simulator
// Output on an iPhone
@"iPhone1,1" on iPhone
@"iPhone1,2" on iPhone 3G
@"iPhone2,1" on iPhone 3GS
@"iPhone3,1" on iPhone 4 (GSM)
@"iPhone3,2" on iPhone 4 (GSM Rev A)
@"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)
@"iPhone11,2" on iPhone XS
@"iPhone11,4" on iPhone XS Max
@"iPhone11,6" on iPhone XS Max China
@"iPhone11,8" on iPhone XR
@"iPhone12,1" on iPhone 11
@"iPhone12,3" on iPhone 11 Pro
@"iPhone12,5" on iPhone 11 Pro Max
@"iPhone12,8" on iPhone SE (2nd Gen)
@"iPhone13,1" on iPhone 12 Mini
@"iPhone13,2" on iPhone 12
@"iPhone13,3" on iPhone 12 Pro
@"iPhone13,4" on iPhone 12 Pro Max
//iPad 1
@"iPad1,1" on iPad - Wifi (model A1219)
@"iPad1,2" 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 9.7"
@"iPad6,3" - Wifi (model A1673)
@"iPad6,4" - Wifi + Cellular (model A1674)
@"iPad6,4" - Wifi + Cellular (model A1675)
//iPad PRO 12.9"
@"iPad6,7" - Wifi (model A1584)
@"iPad6,8" - Wifi + Cellular (model A1652)
//iPad (5th generation)
@"iPad6,11" - Wifi (model A1822)
@"iPad6,12" - Wifi + Cellular (model A1823)
//iPad PRO 12.9" (2nd Gen)
@"iPad7,1" - Wifi (model A1670)
@"iPad7,2" - Wifi + Cellular (model A1671)
@"iPad7,2" - Wifi + Cellular (model A1821)
//iPad PRO 10.5"
@"iPad7,3" - Wifi (model A1701)
@"iPad7,4" - Wifi + Cellular (model A1709)
// iPad (6th Gen)
@"iPad7,5" - WiFi
@"iPad7,6" - WiFi + Cellular
// iPad (7th Gen)
@"iPad7,11" - WiFi
@"iPad7,12" - WiFi + Cellular
//iPad PRO 11"
@"iPad8,1" - WiFi
@"iPad8,2" - 1TB, WiFi
@"iPad8,3" - WiFi + Cellular
@"iPad8,4" - 1TB, WiFi + Cellular
//iPad PRO 12.9" (3rd Gen)
@"iPad8,5" - WiFi
@"iPad8,6" - 1TB, WiFi
@"iPad8,7" - WiFi + Cellular
@"iPad8,8" - 1TB, WiFi + Cellular
//iPad PRO 11" (2nd Gen)
@"iPad8,9" - WiFi
@"iPad8,10" - 1TB, WiFi
//iPad PRO 12.9" (4th Gen)
@"iPad8,11" - (WiFi)
@"iPad8,12" - (WiFi+Cellular)
// iPad mini 5th Gen
@"iPad11,1" - WiFi
@"iPad11,2" - Wifi + Cellular
// iPad Air 3rd Gen
@"iPad11,3" - Wifi
@"iPad11,4" - Wifi + Cellular
// iPad (8th Gen)
@"iPad11,6" - iPad 8th Gen (WiFi)
@"iPad11,7" - iPad 8th Gen (WiFi+Cellular)
// iPad Air 4th Gen
@"iPad13,1" - iPad air 4th Gen (WiFi)
@"iPad13,2" - iPad air 4th Gen (WiFi+Cellular)
//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
@"iPod5,1" on iPod Touch 5th Generation
@"iPod7,1" on iPod Touch 6th Generation
@"iPod9,1" on iPod Touch 7th Generation
// Apple Watch
@"Watch1,1" on Apple Watch 38mm case
@"Watch1,2" on Apple Watch 38mm case
@"Watch2,6" on Apple Watch Series 1 38mm case
@"Watch2,7" on Apple Watch Series 1 42mm case
@"Watch2,3" on Apple Watch Series 2 38mm case
@"Watch2,4" on Apple Watch Series 2 42mm case
@"Watch3,1" on Apple Watch Series 3 38mm case (GPS+Cellular)
@"Watch3,2" on Apple Watch Series 3 42mm case (GPS+Cellular)
@"Watch3,3" on Apple Watch Series 3 38mm case (GPS)
@"Watch3,4" on Apple Watch Series 3 42mm case (GPS)
@"Watch4,1" on Apple Watch Series 4 40mm case (GPS)
@"Watch4,2" on Apple Watch Series 4 44mm case (GPS)
@"Watch4,3" on Apple Watch Series 4 40mm case (GPS+Cellular)
@"Watch4,4" on Apple Watch Series 4 44mm case (GPS+Cellular)
@"Watch5,1" on Apple Watch Series 5 40mm case (GPS)
@"Watch5,2" on Apple Watch Series 5 44mm case (GPS)
@"Watch5,3" on Apple Watch Series 5 40mm case (GPS+Cellular)
@"Watch5,4" on Apple Watch Series 5 44mm case (GPS+Cellular)
@"Watch5,9" on Apple Watch SE 40mm case (GPS)
@"Watch5,10" on Apple Watch SE 44mm case (GPS)
@"Watch5,11" on Apple Watch SE 40mm case (GPS+Cellular)
@"Watch5,12" on Apple Watch SE 44mm case (GPS+Cellular)
@"Watch6,1" on Apple Watch Series 6 40mm case (GPS)
@"Watch6,2" on Apple Watch Series 6 44mm case (GPS)
@"Watch6,3" on Apple Watch Series 6 40mm case (GPS+Cellular)
@"Watch6,4" on Apple Watch Series 6 44mm case (GPS+Cellular)
Solution 2:
Expanding on OhhMee's answer above, I added some failsafe to support future devices not (yet) included on the list:
#import <sys/utsname.h>
#import "MyClass.h"
@implementation MyClass
{
//(your private ivars)
}
- (NSString*) deviceName
{
struct utsname systemInfo;
uname(&systemInfo);
NSString* code = [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
static NSDictionary* deviceNamesByCode = nil;
if (!deviceNamesByCode) {
deviceNamesByCode = @{@"i386" : @"Simulator",
@"x86_64" : @"Simulator",
@"iPod1,1" : @"iPod Touch", // (Original)
@"iPod2,1" : @"iPod Touch", // (Second Generation)
@"iPod3,1" : @"iPod Touch", // (Third Generation)
@"iPod4,1" : @"iPod Touch", // (Fourth Generation)
@"iPod7,1" : @"iPod Touch", // (6th Generation)
@"iPhone1,1" : @"iPhone", // (Original)
@"iPhone1,2" : @"iPhone", // (3G)
@"iPhone2,1" : @"iPhone", // (3GS)
@"iPad1,1" : @"iPad", // (Original)
@"iPad2,1" : @"iPad 2", //
@"iPad3,1" : @"iPad", // (3rd Generation)
@"iPhone3,1" : @"iPhone 4", // (GSM)
@"iPhone3,3" : @"iPhone 4", // (CDMA/Verizon/Sprint)
@"iPhone4,1" : @"iPhone 4S", //
@"iPhone5,1" : @"iPhone 5", // (model A1428, AT&T/Canada)
@"iPhone5,2" : @"iPhone 5", // (model A1429, everything else)
@"iPad3,4" : @"iPad", // (4th Generation)
@"iPad2,5" : @"iPad Mini", // (Original)
@"iPhone5,3" : @"iPhone 5c", // (model A1456, A1532 | GSM)
@"iPhone5,4" : @"iPhone 5c", // (model A1507, A1516, A1526 (China), A1529 | Global)
@"iPhone6,1" : @"iPhone 5s", // (model A1433, A1533 | GSM)
@"iPhone6,2" : @"iPhone 5s", // (model A1457, A1518, A1528 (China), A1530 | Global)
@"iPhone7,1" : @"iPhone 6 Plus", //
@"iPhone7,2" : @"iPhone 6", //
@"iPhone8,1" : @"iPhone 6S", //
@"iPhone8,2" : @"iPhone 6S Plus", //
@"iPhone8,4" : @"iPhone SE", //
@"iPhone9,1" : @"iPhone 7", //
@"iPhone9,3" : @"iPhone 7", //
@"iPhone9,2" : @"iPhone 7 Plus", //
@"iPhone9,4" : @"iPhone 7 Plus", //
@"iPhone10,1": @"iPhone 8", // CDMA
@"iPhone10,4": @"iPhone 8", // GSM
@"iPhone10,2": @"iPhone 8 Plus", // CDMA
@"iPhone10,5": @"iPhone 8 Plus", // GSM
@"iPhone10,3": @"iPhone X", // CDMA
@"iPhone10,6": @"iPhone X", // GSM
@"iPhone11,2": @"iPhone XS", //
@"iPhone11,4": @"iPhone XS Max", //
@"iPhone11,6": @"iPhone XS Max", // China
@"iPhone11,8": @"iPhone XR", //
@"iPhone12,1": @"iPhone 11", //
@"iPhone12,3": @"iPhone 11 Pro", //
@"iPhone12,5": @"iPhone 11 Pro Max", //
@"iPad4,1" : @"iPad Air", // 5th Generation iPad (iPad Air) - Wifi
@"iPad4,2" : @"iPad Air", // 5th Generation iPad (iPad Air) - Cellular
@"iPad4,4" : @"iPad Mini", // (2nd Generation iPad Mini - Wifi)
@"iPad4,5" : @"iPad Mini", // (2nd Generation iPad Mini - Cellular)
@"iPad4,7" : @"iPad Mini", // (3rd Generation iPad Mini - Wifi (model A1599))
@"iPad6,7" : @"iPad Pro (12.9\")", // iPad Pro 12.9 inches - (model A1584)
@"iPad6,8" : @"iPad Pro (12.9\")", // iPad Pro 12.9 inches - (model A1652)
@"iPad6,3" : @"iPad Pro (9.7\")", // iPad Pro 9.7 inches - (model A1673)
@"iPad6,4" : @"iPad Pro (9.7\")" // iPad Pro 9.7 inches - (models A1674 and A1675)
};
}
NSString* deviceName = [deviceNamesByCode objectForKey:code];
if (!deviceName) {
// Not found on database. At least guess main device type from string contents:
if ([code rangeOfString:@"iPod"].location != NSNotFound) {
deviceName = @"iPod Touch";
}
else if([code rangeOfString:@"iPad"].location != NSNotFound) {
deviceName = @"iPad";
}
else if([code rangeOfString:@"iPhone"].location != NSNotFound){
deviceName = @"iPhone";
}
else {
deviceName = @"Unknown";
}
}
return deviceName;
}
// (rest of class implementation omitted)
@end
I also omitted the detailed information (e.g. "model A1507, A1516, A1526 (China), A1529 | Global") and placed it in the comments instead, in case you want to use this as user-facing strings and not freak them out.
Edit: This answer provides a similar implementation using Swift 2.
Edit 2: I just added the iPad Pro models (both sizes). For future reference, the model numbers/etc. can be found in The iPhone Wiki.
Edit 3: Add support for iPhone XS, iPhone XS Max and iPhone XR.
Edit 4: Add support for iPhone 11, iPhone 11 Pro and iPhone 11 Pro Max.
Solution 3:
I've made the plist file to help you to get the complete name for each device (source code right at the end of my answer)
Based on OhhMee's answer, you can use it like this:
Swift 4.0
static func deviceName() -> String {
var systemInfo = utsname()
uname(&systemInfo)
guard let iOSDeviceModelsPath = Bundle.main.path(forResource: "iOSDeviceModelMapping", ofType: "plist") else { return "" }
guard let iOSDevices = NSDictionary(contentsOfFile: iOSDeviceModelsPath) else { return "" }
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 iOSDevices.value(forKey: identifier) as! String
}
Don't forget to add #import <sys/utsname.h>
in your Bridging Header.
Objective C
#import <sys/utsname.h>
NSString *machineName()
{
struct utsname systemInfo;
uname(&systemInfo);
NSString *iOSDeviceModelsPath = [[NSBundle mainBundle] pathForResource:@"iOSDeviceModelMapping" ofType:@"plist"];
NSDictionary *iOSDevices = [NSDictionary dictionaryWithContentsOfFile:iOSDeviceModelsPath];
NSString* deviceModel = [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
return [iOSDevices valueForKey:deviceModel];
}
The plist file :
<?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>x86_64</key>
<string>Simulator</string>
<key>i386</key>
<string>Simulator</string>
<key>iPod1,1</key>
<string>iPod Touch 1st Gen</string>
<key>iPod2,1</key>
<string>iPod Touch 2nd Gen</string>
<key>iPod3,1</key>
<string>iPod Touch 3rd Gen</string>
<key>iPod4,1</key>
<string>iPod Touch 4th Gen</string>
<key>iPod5,1</key>
<string>iPod Touch 5th Gen</string>
<key>iPod7,1</key>
<string>iPod Touch 6th Gen</string>
<key>iPhone1,1</key>
<string>iPhone</string>
<key>iPhone1,2</key>
<string>iPhone 3G</string>
<key>iPhone2,1</key>
<string>iPhone 3GS</string>
<key>iPhone3,1</key>
<string>iPhone 4</string>
<key>iPhone3,2</key>
<string>iPhone 4</string>
<key>iPhone3,3</key>
<string>iPhone 4</string>
<key>iPhone4,1</key>
<string>iPhone 4S</string>
<key>iPhone5,1</key>
<string>iPhone 5 model A1428</string>
<key>iPhone5,2</key>
<string>iPhone 5 model A1429</string>
<key>iPhone5,3</key>
<string>iPhone 5C</string>
<key>iPhone5,4</key>
<string>iPhone 5C</string>
<key>iPhone6,1</key>
<string>iPhone 5S</string>
<key>iPhone6,2</key>
<string>iPhone 5S</string>
<key>iPhone7,2</key>
<string>iPhone 6</string>
<key>iPhone7,1</key>
<string>iPhone 6 Plus</string>
<key>iPhone8,1</key>
<string>iPhone 6S</string>
<key>iPhone8,2</key>
<string>iPhone 6S Plus</string>
<key>iPhone8,4</key>
<string>iPhone SE</string>
<key>iPhone9,1</key>
<string>iPhone 7</string>
<key>iPhone9,2</key>
<string>iPhone 7 Plus</string>
<key>iPhone9,3</key>
<string>iPhone 7</string>
<key>iPhone9,4</key>
<string>iPhone 7 Plus</string>
<key>iPhone10,1</key>
<string>iPhone 8</string>
<key>iPhone10,4</key>
<string>iPhone 8</string>
<key>iPhone10,2</key>
<string>iPhone 8 Plus</string>
<key>iPhone10,5</key>
<string>iPhone 8 Plus</string>
<key>iPhone10,3</key>
<string>iPhone X</string>
<key>iPhone10,6</key>
<string>iPhone X</string>
<key>iPhone11,2</key>
<string>iPhone XS</string>
<key>iPhone11,4</key>
<string>iPhone XS Max</string>
<key>iPhone11,6</key>
<string>iPhone XS Max</string>
<key>iPhone11,8</key>
<string>iPhone XR</string>
<key>iPad1,1</key>
<string>iPad</string>
<key>iPad2,1</key>
<string>iPad 2</string>
<key>iPad2,2</key>
<string>iPad 2</string>
<key>iPad2,3</key>
<string>iPad 2</string>
<key>iPad2,4</key>
<string>iPad 2</string>
<key>iPad3,1</key>
<string>iPad 3rd Gen</string>
<key>iPad3,2</key>
<string>iPad 3rd Gen</string>
<key>iPad3,3</key>
<string>iPad 3rd Gen</string>
<key>iPad3,4</key>
<string>iPad 4th Gen</string>
<key>iPad3,5</key>
<string>iPad 4th Gen</string>
<key>iPad3,6</key>
<string>iPad 4th Gen</string>
<key>iPad4,1</key>
<string>iPad Air</string>
<key>iPad4,2</key>
<string>iPad Air</string>
<key>iPad4,3</key>
<string>iPad Air</string>
<key>iPad2,5</key>
<string>iPad Mini 1st Gen</string>
<key>iPad2,6</key>
<string>iPad Mini 1st Gen</string>
<key>iPad2,7</key>
<string>iPad Mini 1st Gen</string>
<key>iPad4,4</key>
<string>iPad Mini 2nd Gen</string>
<key>iPad4,5</key>
<string>iPad Mini 2nd Gen</string>
<key>iPad4,6</key>
<string>iPad Mini 2nd Gen</string>
<key>iPad4,7</key>
<string>iPad Mini 3rd Gen</string>
<key>iPad4,8</key>
<string>iPad Mini 3rd Gen</string>
<key>iPad4,9</key>
<string>iPad Mini 3rd Gen</string>
<key>iPad5,1</key>
<string>iPad Mini 4</string>
<key>iPad5,2</key>
<string>iPad Mini 4</string>
<key>iPad5,3</key>
<string>iPad Air 2</string>
<key>iPad5,4</key>
<string>iPad Air 2</string>
<key>iPad6,3</key>
<string>iPad Pro 9.7 inch</string>
<key>iPad6,4</key>
<string>iPad Pro 9.7 inch</string>
<key>iPad6,7</key>
<string>iPad Pro 12.9 inch</string>
<key>iPad6,8</key>
<string>iPad Pro 12.9 inch</string>
<key>iPad7,1</key>
<string>iPad Pro 12.9 inch 2nd Gen</string>
<key>iPad7,2</key>
<string>iPad Pro 12.9 inch 2nd Gen</string>
<key>iPad7,3</key>
<string>iPad Pro 10.5 inch</string>
<key>iPad7,4</key>
<string>iPad Pro 10.5 inch</string>
<key>iPad8,1</key>
<string>iPad Pro 11 inch</string>
<key>iPad8,2</key>
<string>iPad Pro 11 inch</string>
<key>iPad8,3</key>
<string>iPad Pro 11 inch</string>
<key>iPad8,4</key>
<string>iPad Pro 11 inch</string>
<key>iPad8,5</key>
<string>iPad Pro 12.9 inch 3rd Gen</string>
<key>iPad8,6</key>
<string>iPad Pro 12.9 inch 3rd Gen</string>
<key>iPad8,7</key>
<string>iPad Pro 12.9 inch 3rd Gen</string>
<key>iPad8,8</key>
<string>iPad Pro 12.9 inch 3rd Gen</string>
</dict>
</plist>
Solution 4:
[[UIDevice currentDevice] model]
just returns iPhone
or iPod
, you don't get the numbers of the model that would let you differentiate between different generations of devices.
Code this method:
#include <sys/sysctl.h>
...
+ (NSString *)getModel {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *model = malloc(size);
sysctlbyname("hw.machine", model, &size, NULL, 0);
NSString *deviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
free(model);
return deviceModel;
}
And call the method [self getModel]
whenever you need the model and you'll get i.e: "iPhone5,1" for the ridiculously thin and speedy iPhone 5.
One good practice is to create a class called Utils.h/Utils.m
and put methods like getModel
there so you can get this info from anywhere in your App simply by importing the class and invoking [Utils getModel];
Solution 5:
I've optimized NicolasMiari's implementation, and added Simulator
, here:
+ (NSString*)deviceName {
static NSDictionary* deviceNamesByCode = nil;
static NSString* deviceName = nil;
if (deviceName) {
return deviceName;
}
deviceNamesByCode = @{
@"i386" :@"Simulator",
@"iPod1,1" :@"iPod Touch", // (Original)
@"iPod2,1" :@"iPod Touch", // (Second Generation)
@"iPod3,1" :@"iPod Touch", // (Third Generation)
@"iPod4,1" :@"iPod Touch", // (Fourth Generation)
@"iPhone1,1" :@"iPhone", // (Original)
@"iPhone1,2" :@"iPhone", // (3G)
@"iPhone2,1" :@"iPhone", // (3GS)
@"iPad1,1" :@"iPad", // (Original)
@"iPad2,1" :@"iPad 2", //
@"iPad3,1" :@"iPad", // (3rd Generation)
@"iPhone3,1" :@"iPhone 4", //
@"iPhone4,1" :@"iPhone 4S", //
@"iPhone5,1" :@"iPhone 5", // (model A1428, AT&T/Canada)
@"iPhone5,2" :@"iPhone 5", // (model A1429, everything else)
@"iPad3,4" :@"iPad", // (4th Generation)
@"iPad2,5" :@"iPad Mini", // (Original)
@"iPhone5,3" :@"iPhone 5c", // (model A1456, A1532 | GSM)
@"iPhone5,4" :@"iPhone 5c", // (model A1507, A1516, A1526 (China), A1529 | Global)
@"iPhone6,1" :@"iPhone 5s", // (model A1433, A1533 | GSM)
@"iPhone6,2" :@"iPhone 5s", // (model A1457, A1518, A1528 (China), A1530 | Global)
@"iPad4,1" :@"iPad Air", // 5th Generation iPad (iPad Air) - Wifi
@"iPad4,2" :@"iPad Air", // 5th Generation iPad (iPad Air) - Cellular
@"iPad4,4" :@"iPad Mini", // (2nd Generation iPad Mini - Wifi)
@"iPad4,5" :@"iPad Mini" // (2nd Generation iPad Mini - Cellular)
};
struct utsname systemInfo;
uname(&systemInfo);
NSString* code = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
deviceName = [deviceNamesByCode objectForKey:code];
if (!deviceName) {
// Not found in database. At least guess main device type from string contents:
if ([code rangeOfString:@"iPod"].location != NSNotFound) {
deviceName = @"iPod Touch";
} else if([code rangeOfString:@"iPad"].location != NSNotFound) {
deviceName = @"iPad";
} else if([code rangeOfString:@"iPhone"].location != NSNotFound){
deviceName = @"iPhone";
} else {
deviceName = @"Simulator";
}
}
return deviceName;
}
```