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?
There are some problems with the accepted answer when you're using Swift 3! This answer (inspired from NAZIK) works with Swift 3 and the new iPhone models:
Below is the code for getting the hardware string, but you need to compare these hardware string to know which device it is. I have created a class for that contains almost all the device strings(we're keeping string upto date with new devices). It's easy to use please check
Swift : GitHub/DeviceGuru
Objective-C : GitHub/DeviceUtil
You can use BDLocalizedDevicesModels framework to parse device info and get the name.
Then just call
UIDevice.currentDevice.productName
in your code.Swift 4 both device/simulator
and new iPhone XR, XS, XS Max
P.S. ( This method detect also the correct model if it's a simulator and the simulator device model used..):
Usage: you get your model simply with:
or print the exact string with:
Output:
Other example:
For Apple devices modeles visit: https://www.theiphonewiki.com/wiki/Models
Using Swift 3 (Xcode 8.3)
Note: According to official dev forum answer, it is safe to use tuples in this way. Memory alignment for the big Int8 tuple will be the same as if it were a big Int8 array. ie: contiguous and not-padded.
I've made another sample extension on UIDevice to include simulator model identifier base on @HAS's answer :
Swift 4.x version:
It's working fine with Swift3.2 above: