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 utsname
as the structure returned by theuname
function. Theuname
function returns information about the current system.The identifier
systemInfo
is just a variable name. It has no special meaning.The “need to convert” exists because
utsname.machine
contains 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.machine
string. If you want to refer to the user's device by its user-friendly name, you need to convert from theutsname.machine
string to the user-friendly name.
References
SO | detect-current-device-with-ui-user-interface-idiom-in-swift