Is there a way to get the device model name (iPhone 4S, iPhone 5, iPhone 5S, etc) in Swift?
I know there is a property named UIDevice.currentDevice().model
but it only returns device type (iPod touch, iPhone, iPad, iPhone Simulator, etc).
I also know it can be done easily in Objective-C with this method:
#import <sys/utsname.h>
struct utsname systemInfo;
uname(&systemInfo);
NSString* deviceModel = [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
But I'm developing my iPhone app in Swift so could someone please help me with the equivalent way to solve this in Swift?
Best Solution
I made this "pure Swift" extension on
UIDevice
.This version works in Swift 2 or higher If you use an earlier version please use an older version of my answer.
If you are looking for a more elegant solution you can use my ยต-framework
DeviceKit
published on GitHub (also available via CocoaPods, Carthage and Swift Package Manager).Here's the code:
You call it like this:
For real devices it returns e.g. "iPad Pro 9.7 Inch", for simulators it returns "Simulator " + Simulator identifier, e.g. "Simulator iPad Pro 9.7 Inch"