Device
Code
UIDevice.current.userInterfaceIdiom == .pad
UIDevice.current.userInterfaceIdiom == .phone
UIDevice.current.userInterfaceIdiom == .unspecified
Model
public extension UIDevice {
static let modelName: 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 { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
}
POSIX defines
struct utsnameas the structure returned by theunamefunction. Theunamefunction returns information about the current system.The identifier
systemInfois just a variable name. It has no special meaning.The “need to convert” exists because
utsname.machinecontains a string intended for programs, programmers and technicians, not end users. There have been multiple hardware versions of certain devices, like the iPhone 5S. Apple calls all of them “iPhone 5S” when speaking to users. Internally, you can tell which hardware version you're on by looking at theutsname.machinestring. If you want to refer to the user's device by its user-friendly name, you need to convert from theutsname.machinestring to the user-friendly name.
References
SO | detect-current-device-with-ui-user-interface-idiom-in-swift