How to get device make and model on iOS?

后端 未结 19 1200
爱一瞬间的悲伤
爱一瞬间的悲伤 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:10

    Swift 4 or later

    extension UIDevice {
        var modelName: String {
            if let modelName = ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"] { return modelName }
            var info = utsname()
            uname(&info)
            return String(String.UnicodeScalarView(
                       Mirror(reflecting: info.machine)
                        .children
                        .compactMap {
                            guard let value = $0.value as? Int8 else { return nil }
                            let unicode = UnicodeScalar(UInt8(value))
                            return unicode.isASCII ? unicode : nil
                        }))
        }
    }
    

    UIDevice.current.modelName   // "iPad6,4"
    

提交回复
热议问题