Get model info of iOS devices

iOS Tech Set
iOS App Development
2 min readNov 26, 2018

To know the customers’ device info is often regarded as a way to analysis the app’s performance and measure customers’ usage. However, Apple does not officially provide a direct API to decide which device the customer is using. Based on such case, how can we know it is an iPhone X, iPhone 8, iPad, or Apple TV?

UIDevice class is the entry point to get the info of current device. To determine the device model of an iOS device, we can fetch that info from model property — a string describes the model type, such as “iPhone” or “iPod Touch”.

There is also another property name tells the name identifying the device. However, the name is often a random string such as “My iPhone XX”, or “XX’s iPad”; this value can also be modified by users by going to “Settings” -> “General” -> “About” -> “Name” on his/her own device. In this case, we cannot depend on this name property to get the specific information.

To get more specific details of a device, such as whether it is an iPhone XR or iPhone XS, we need some tricky ways to fetch the info. As we all know, every iOS device has a hardware identifier correspond to itself, which we call it “Device Model” or “Machine Name” and looks like “iPhone7,3” or “AppleTV5,3”. We can get them by following approach:

This function returns device model as a string. However, it is not something like “iPhone X” or “Apple TV 4”. So the next step is to convert the device model to readable string. In order to do so, we need a mapping function and we could reference here for details:

With above mapping definitions, the whole implementation could look like this:

Then you can call the following to get the model name of the device.

let modelName = UIDevice.modelName

Another suggestion is to put the mapping function in backend server; hence iOS side just needs to call the API to satisfy the model name conversion. In this way, we do not need to update the mapping function in the local side when new devices launched, since the code block is lengthy and the modification is highly error prone.

--

--