How to determine device type (iPhone, iPod Touch) with iPhone SDK?

Mobile DevelopmentiOSiPhone/iPad

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

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;
   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()
   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,1iPod Touch 4G
iPod5,1iPod Touch 5G
iPod7,1iPod Touch 6G
iPhone3,1 , iPhone3,2 , iPhone3,3iPhone 4
iPhone4iPhone 4s
iPhone7,2iPhone 6
iPhone7,1iPhone 6 Plus
iPhone8,1iPhone 6s
iPhone8,2iPhone 6s Plus
iPhone8,4iPhone SE
iPhone9,1 , iPhone9,3iPhone 7
iPhone9,2 , iPhone 9,4iPhone 7 Plus
iPad2,1 , iPad2,2 , iPad2,3 , iPad2,4iPad 2
iPad3,1 , iPad3,2 , iPad3,3iPad 3
iPad3,4 , iPad3,5 , iPad3,6iPad 4
iPad4,1 , iPad4,2 , iPad4,3iPad Air
iPad5,3 , iPad5,4iPad Air 2
iPad2,5 , iPad2,6 , iPad2,7iPad Mini
iPad4,4 , iPad4,5 , iPad4,6iPad Mini 2
iPad4,7 , iPad4,8 , iPad4,9iPad Mini 3
iPad5,1 , iPad5,2iPad Mini 4
iPad6,3 , iPad6,4iPad Pro (9.7 inch)
iPad6,7 , iPad6,8iPad Pro (12.9 inch)
Published on 28-Dec-2018 09:50:07