When working on iOS applications, we sometimes need to know the device on which application is installed, and provide custom features depending on the device in use. For example, we want to provide some features on iPhone X but not on iPhone 7. In this article, we’ll learn how to find the iOS device being used, using an iOS Application.
Let’s go through some terms that will be required for achieving the desired results,
utsname − this is a structure located in Darwin module of iOS
uname − uname is a function that takes utsname as an input and returns Int32 as output.
Finding the device in use on iOS SDK is a simple task, if we just need to find the model or the name we can use any of the below codes, and it will give us a string in return.
- UIDevice.current.localizedModel
- UIDevice.current.model
- UIDevice.current.name
But the above code does not the exact device in use, it only tells us that an iPhone is being used. To get the exact device in use we’ll have to go through some steps.
In objective C we can do it like
NSString* deviceName() { struct utsname systemInfo; uname(&systemInfo); return [NSString stringWithCString:systemInfo.machine, encoding:NSUTF8StringEncoding]; }
Mirror − A representation of the substructure and display style of an instance of any type.
In Swift, we’ll use all of the above mentioned terms to get device number as an output.
func getDevice() -> 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 { identifier } identifier + String(UnicodeScalar(UInt8(value))) } return identifier }
This function gives us the model code but not exactly the device name.
We’ll have to compare this “identifier” variable against multiple values to get model name as per the requirement.
Following is the result while running this code on iPhone 7 plus simulator.
Some of the model codes for apple devices and their respective names are −
iPod4,1 | iPod Touch 4G |
iPod5,1 | iPod Touch 5G |
iPod7,1 | iPod Touch 6G |
iPhone3,1 , iPhone3,2 , iPhone3,3 | iPhone 4 |
iPhone4 | iPhone 4s |
iPhone7,2 | iPhone 6 |
iPhone7,1 | iPhone 6 Plus |
iPhone8,1 | iPhone 6s |
iPhone8,2 | iPhone 6s Plus |
iPhone8,4 | iPhone SE |
iPhone9,1 , iPhone9,3 | iPhone 7 |
iPhone9,2 , iPhone 9,4 | iPhone 7 Plus |
iPad2,1 , iPad2,2 , iPad2,3 , iPad2,4 | iPad 2 |
iPad3,1 , iPad3,2 , iPad3,3 | iPad 3 |
iPad3,4 , iPad3,5 , iPad3,6 | iPad 4 |
iPad4,1 , iPad4,2 , iPad4,3 | iPad Air |
iPad5,3 , iPad5,4 | iPad Air 2 |
iPad2,5 , iPad2,6 , iPad2,7 | iPad Mini |
iPad4,4 , iPad4,5 , iPad4,6 | iPad Mini 2 |
iPad4,7 , iPad4,8 , iPad4,9 | iPad Mini 3 |
iPad5,1 , iPad5,2 | iPad Mini 4 |
iPad6,3 , iPad6,4 | iPad Pro (9.7 inch) |
iPad6,7 , iPad6,8 | iPad Pro (12.9 inch) |