
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to determine device type (iPhone, iPod Touch) with Swift?
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.
Mirror − A representation of the substructure and display style of an instance of any type.
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.
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 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.
Here is an example of how it looks when we run the code on a simulator running iPhone running iPhone 7 Plus on an iOS 12.0 Version
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 |
iPhone5,1 , iPhone5,2 | iPhone 5 |
iPhone5,3 , iPhone5,4 | iPhone 5c |
iPhone6,1 , iPhone6,2 | iPhone 5s |
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) |
- Related Articles
- How to determine device type (iPhone, iPod Touch) with iPhone SDK?
- How to generate Unique ID of device for iPhone/iPad using Swift?
- How to make iPhone vibrate using Swift?
- How to determine the current iPhone model information?
- How to get the touch position on android device?
- How to get the Touch position on iOS device?
- How to make a USB device multi-touch enabled
- Detect current device with UIUserInterfaceIdiom in Swift
- How to get the Touch position on an android device using kotlin?
- How to get a unique device ID in Swift?
- How to provide a localized description with an Error type in Swift?
- How to determine if running on a rooted device or not in Android?
- How to determine Android device screen size category (small, normal, large, xlarge) programatically?
- How to check if a bluetooth device is connected with Android device?
- How to detect a mobile device with JavaScript?
