简体   繁体   中英

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). But I want to determine the Apple device model number, not model name. It should be a string like A1823 or A2404.

If you don't bother fiddling with private APIs then

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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