Last active
December 7, 2017 19:25
-
-
Save bielikb/b969a56d4e0853e3faed4170dea5f710 to your computer and use it in GitHub Desktop.
UIDevice extension with model type - iPhone X support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIDevice { | |
enum UIDeviceModelType : Equatable { | |
///iPhoneX | |
case iPhoneX | |
///Other models | |
case other(model: String) | |
static func type(from model: String) -> UIDeviceModelType { | |
switch model { | |
case "iPhone10,3", "iPhone10,6": | |
return .iPhoneX | |
default: | |
return .other(model: model) | |
} | |
} | |
static func ==(lhs: UIDeviceModelType, rhs: UIDeviceModelType) -> Bool { | |
switch (lhs, rhs) { | |
case (.iPhoneX, .iPhoneX): | |
return true | |
case (.other(let modelOne), .other(let modelTwo)): | |
return modelOne == modelTwo | |
default: | |
return false | |
} | |
} | |
} | |
var simulatorModel: String? { | |
guard TARGET_OS_SIMULATOR != 0 else { | |
return nil | |
} | |
return ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"] | |
} | |
var hardwareModel: String { | |
var systemInfo = utsname() | |
uname(&systemInfo) | |
let machineMirror = Mirror(reflecting: systemInfo.machine) | |
let model = machineMirror.children.reduce("") { identifier, element in | |
guard let value = element.value as? Int8, value != 0 else { return identifier } | |
return identifier + String(UnicodeScalar(UInt8(value))) | |
} | |
return model | |
} | |
var modelType: UIDeviceModelType { | |
let model = self.simulatorModel ?? self.hardwareModel | |
return UIDeviceModelType.type(from: model) | |
} | |
var isIPhoneX: Bool { | |
return modelType == .iPhoneX | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try 😉