0

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.

Roman Podymov
  • 4,168
  • 4
  • 30
  • 57

1 Answers1

0

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
    }
}
egghese
  • 2,193
  • 16
  • 26
  • Thanks. But for my iPad mini 4 `UIDevice.current.modelNumber` returns **MK9P2** when I expect **A1538**. – Roman Podymov Feb 22 '21 at 09:55
  • 1
    Oh, that seems to be the Order Number. I can't quite figure out what is the correct Key to get the Model Number as you want. Here you can find [Order Number mapping to Model Number](https://everymac.com/systems/apple/ipad/specs/apple-ipad-mini-4-a1538-wi-fi-only-specs.html). Please note that, your app might get rejected on AppStore if you are using this API – egghese Feb 22 '21 at 12:17