Understanding the specifics of a user’s device is crucial for providing tailored support, optimizing application performance, and ensuring compatibility. For iOS developers, accessing the device’s make and model programmatically is a common requirement. This process allows you to adapt your application to specific hardware capabilities, screen sizes, and operating system versions. Knowing how to get device make and model on iOS efficiently will enhance your user experience and streamline development processes. This comprehensive guide will walk you through the various methods and techniques available for retrieving this essential information, ensuring you can implement robust and adaptable solutions for your iOS applications. We’ll cover both Swift and Objective-C approaches and address potential pitfalls to watch out for.
Why Accessing Device Information Matters
Accessing device information, specifically the make and model, plays a pivotal role in several aspects of iOS development. Firstly, it enables developers to optimize application performance based on the device’s processing power and memory capacity. Older devices may require different resource management strategies compared to newer ones. Secondly, device-specific bug fixes and feature implementations become significantly easier when you can identify the exact hardware experiencing issues. For example, if a rendering bug appears only on iPhone X models, knowing this immediately narrows down the scope of debugging. Finally, providing targeted support and troubleshooting becomes more efficient. Imagine a user reporting a problem; knowing their device model beforehand allows support teams to offer more relevant and effective solutions. According to Statista, Apple has released dozens of iPhone models since its inception, each with unique characteristics and capabilities [1]. Understanding these nuances is vital for creating a seamless user experience.
Beyond application development, accessing device information also aids in analytics and user behavior tracking. By aggregating data on the device models using your app, you gain insights into your user base’s hardware preferences. This data can inform future development decisions, such as prioritizing features for the most popular devices or optimizing performance for those with older hardware. Moreover, device information is essential for A/B testing. You can segment your user base by device model and test different features or UI designs to determine which performs best on specific hardware. This data-driven approach ensures that you’re making informed decisions based on real-world user behavior. This helps to enhance user experience and increase user engagement.
In summary, accessing device information is not just about knowing what hardware your application is running on; it’s about leveraging that knowledge to create a better, more optimized, and more user-friendly experience. From targeted bug fixes to data-driven development decisions, the ability to accurately identify the device make and model on iOS is an invaluable asset for any iOS developer. It allows for a personalized experience for each user, ensuring they are getting the most out of the application on their specific device. This ultimately leads to higher user satisfaction and better app ratings.
Methods for Retrieving Device Make and Model
There are several methods to retrieve the device make and model on iOS, each with its own advantages and disadvantages. The most common approach involves using the UIDevice class from the UIKit framework. This class provides access to various device-related information, including the device’s model identifier. However, the raw model identifier returned by UIDevice isn’t always user-friendly (e.g., “iPhone10,3”). Therefore, it’s often necessary to map this identifier to a more readable name, such as “iPhone X.” This mapping is typically done using a lookup table or an external library. We need to convert the machine name into a human-readable format for better understanding. The uname() function from the System.h library can also be used to fetch the device information.
Here’s a breakdown of the common methods:
- Using UIDevice.model (Swift): This method provides a localized version of the device model name, like “iPhone” or “iPad”. While easy to use, it doesn’t give the specific model number.
- Using UIDevice.identifierForVendor (Swift): This method provides a unique identifier for the device that is the same across all apps from the same vendor. This isn’t the model number but is useful for tracking purposes.
- Using UIDevice.systemVersion (Swift): Returns the version of the operating system currently running on the device.
The most accurate way to determine the specific device model is to use the sysctlbyname function to retrieve the “hw.machine” property. This returns the machine name, which can then be mapped to a human-readable model name. For example:
Featured Snippet: To get the exact device model on iOS, use the sysctlbyname function with the argument “hw.machine”. This returns a string representing the device’s hardware identifier, such as “iPhone10,3” for an iPhone X. You then need to map this identifier to a user-friendly name using a lookup table or external library. This method provides the most accurate information about the specific device model, allowing for precise optimization and targeted bug fixes.
Implementing Device Model Retrieval in Swift
Implementing device model retrieval in Swift involves using the UIDevice class and a helper function to map the machine name to a user-friendly model name. The following code snippet demonstrates how to achieve this:
import UIKit public extension UIDevice { static let 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))) } func mapToDevice(identifier: String) -> String { // swiftlint:disable:this cyclomatic_complexity if os(iOS) switch identifier { case "iPod5,1": return "iPod Touch 5" case "iPod7,1": return "iPod Touch 6" case "iPod9,1": return "iPod Touch 7" case "iPhone3,1", "iPhone3,2", "iPhone3,3": return "iPhone 4" case "iPhone4,1": return "iPhone 4s" case "iPhone5,1", "iPhone5,2": return "iPhone 5" case "iPhone5,3", "iPhone5,4": return "iPhone 5c" case "iPhone6,1", "iPhone6,2": return "iPhone 5s" case "iPhone7,2": return "iPhone 6" case "iPhone7,1": return "iPhone 6 Plus" case "iPhone8,1": return "iPhone 6s" case "iPhone8,2": return "iPhone 6s Plus" case "iPhone8,4": return "iPhone SE" case "iPhone9,1", "iPhone9,3": return "iPhone 7" case "iPhone9,2", "iPhone9,4": return "iPhone 7 Plus" case "iPhone10,1", "iPhone10,4": return "iPhone 8" case "iPhone10,2", "iPhone10,5": return "iPhone 8 Plus" case "iPhone10,3", "iPhone10,6": return "iPhone X" case "iPhone11,2": return "iPhone XS" case "iPhone11,4", "iPhone11,6": return "iPhone XS Max" case "iPhone11,8": return "iPhone XR" case "iPhone12,1": return "iPhone 11" case "iPhone12,3": return "iPhone 11 Pro" case "iPhone12,5": return "iPhone 11 Pro Max" case "iPhone12,8": return "iPhone SE (2nd generation)" case "iPhone13,1": return "iPhone 12 mini" case "iPhone13,2": return "iPhone 12" case "iPhone13,3": return "iPhone 12 Pro" case "iPhone13,4": return "iPhone 12 Pro Max" case "iPhone14,4": return "iPhone 13 mini" case "iPhone14,5": return "iPhone 13" case "iPhone14,2": return "iPhone 13 Pro" case "iPhone14,3": return "iPhone 13 Pro Max" case "iPhone14,7": return "iPhone SE (3rd generation)" case "iPhone14,8": return "iPhone 14" case "iPhone14,9": return "iPhone 14 Plus" case "iPhone15,2": return "iPhone 14 Pro" case "iPhone15,3": return "iPhone 14 Pro Max" case "iPhone16,1": return "iPhone 15 Pro" case "iPhone16,2": return "iPhone 15 Pro Max" case "iPhone15,4": return "iPhone 15" case "iPhone15,5": return "iPhone 15 Plus" case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2" case "iPad3,1", "iPad3,2", "iPad3,3": return "iPad (3rd generation)" case "iPad3,4", "iPad3,5", "iPad3,6": return "iPad (4th generation)" case "iPad4,1", "iPad4,2", "iPad4,3": return "iPad Air" case "iPad5,3", "iPad5,4": return "iPad Air 2" case "iPad6,11", "iPad6,12": return "iPad (5th generation)" case "iPad7,5", "iPad7,6": return "iPad (6th generation)" case "iPad7,11", "iPad7,12": return "iPad (7th generation)" case "iPad8,1", "iPad8,2", "iPad8,3", "iPad8,4":return "iPad Pro (11-inch)" case "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8":return "iPad Pro (12.9-inch)" case "iPad11,3", "iPad11,4": return "iPad Air (3rd generation)" case "iPad13,1", "iPad13,2": return "iPad Air (4th generation)" case "iPad13,16", "iPad13,17": return "iPad Air (5th generation)" case "iPad14,1", "iPad14,2": return "iPad mini (6th generation)" case "iPad11,1", "iPad11,2": return "iPad mini (5th generation)" case "iPad6,3", "iPad6,4": return "iPad Pro (9.7-inch)" case "iPad6,7", "iPad6,8": return "iPad Pro (12.9-inch)" case "iPad7,1", "iPad7,2": return "iPad Pro (12.9-inch) (2nd generation)" case "iPad8,9", "iPad8,10": return "iPad Pro (11-inch) (2nd generation)" case "iPad8,11", "iPad8,12": return "iPad Pro (12.9-inch) (4th generation)" case "iPad12,1", "iPad12,2": return "iPad (9th generation)" case "iPad13,4", "iPad13,5", "iPad13,6": return "iPad Pro (11-inch) (3rd generation)" case "iPad13,8", "iPad13,9", "iPad13,10", "iPad13,11": return "iPad Pro (12.9-inch) (5th generation)" case "iPad13,18", "iPad13,19": return "iPad (10th generation)" case "Watch1,1", "Watch1,2": return "Apple Watch (1st generation)" case "Watch2,6", "Watch2,7": return "Apple Watch Series 1" case "Watch2,3", "Watch2,4": return "Apple Watch Series 2" case "Watch3,1", "Watch3,2
<b>Question & Answer : </b><br></br><p>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 through<br></br> NSString *deviceType = [[UIDevice currentDevice] model];<br></br> 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 <em>(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)</em>. </p> <p>So yeah, let me know, thank you!</p>
<br></br><p>EITHER try this library: <a href="http://github.com/erica/uidevice-extension/" rel="noreferrer">http://github.com/erica/uidevice-extension/</a> (by Erica Sadun). (The library is 7-8 years old, and hence is obsolete)</p> <p>(Sample Code):</p> [[UIDevice currentDevice] platformType] // ex: UIDevice4GiPhone [[UIDevice currentDevice] platformString] // ex: @"iPhone 4G" <p>OR You can use this method:</p> <p>You can get the device model number using uname from sys/utsname.h. For example:</p> <p><strong>Objective-C</strong></p> #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]; } <p><strong>Swift 3</strong></p> 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) <p>The result should be:</p> // 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)