How to get device make and model on iOS?

后端 未结 19 1171
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 04:58

I was wondering if it\'s possible to determine what kind of iPhone (for example) the currentdevice is? I know it\'s possible to get the model through
NSString

19条回答
  •  既然无缘
    2020-11-22 05:21

    This solution works for both physical devices and iOS simulator in a way that simulator returns the same model as physical device would return for a model e.g. "iPhone10,6" for iPhone X (GSM) rather than "x86_64".

    Definition - Swift 4:

    import UIKit
    
    extension UIDevice {
        var 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)))
            }
            if let value = ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"] {
                return value
            } else {
                return identifier
            }
        }
    }
    

    Usage:

    print(UIDevice.current.modelName)
    

提交回复
热议问题