简体   繁体   English

如何确定当前苹果设备 model 编号?

[英]How to determine the current Apple device model number?

I know there are a lot of similar questions on Stack Overflow ( this one , for example).我知道 Stack Overflow 上有很多类似的问题(例如这个)。 But I want to determine the Apple device model number, not model name.但我想确定 Apple 设备 model 编号,而不是 model 名称。 It should be a string like A1823 or A2404.它应该是像 A1823 或 A2404 这样的字符串。

If you don't bother fiddling with private APIs then如果您不费心摆弄私有 API,那么

extension UIDevice {
    var modelNumber: String? {
        let getModelNumber: (Selector) -> String? = {
            return UIDevice.current.perform($0, with: "ModelNumber")?.takeUnretainedValue() as? String
        }
        let _deviceInfoKeySel = NSSelectorFromString("_deviceInfoForKey:")
        let deviceInfoKeySel = NSSelectorFromString("deviceInfoForKey:")
        if UIDevice.current.responds(to: _deviceInfoKeySel) {
            return getModelNumber(_deviceInfoKeySel)
        } else if UIDevice.current.responds(to: deviceInfoKeySel) {
            return getModelNumber(deviceInfoKeySel)
        } 
        return nil
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM