# 8. iOS 获取设备型号最新总结

**作者**: [KANGZUBIN](https://weibo.com/kangzubin)

在开发中，我们经常需要获取设备的型号（如 `iPhone X`，`iPhone 8 Plus` 等）以进行数据统计，或者做不同的适配。但苹果并没有提供相应的系统 API 让我们直接取得当前设备的型号。

其中，`UIDevice` 有一个属性 model 只是用于获取 iOS 设备的类型，如 `iPhone`，`iPod touch`，`iPad` 等；而其另一个属性 `name` 表示当前设备的名称，由用户在设置》通用》关于》名称中设定，如 My iPhone，xxx 的 iPhone 等。然而，我们无法根据这两个值获得具体的型号。

不过，每一种 iOS 设备型号都有对应的一个或多个硬件编码/标识符，称为 `device model` 或者叫 machine name，之前的小集介绍过，我们可以通过如下图中的代码来获取。

![](/files/-LUYjo6Pe6HBYp1ypQxy)

所以，通常的做法是，先获取设备的 `device model` 值，再手动映射为具体的设备型号（或者直接把 `device model` 值传给后端，让后端去做映射，这样的好处是可以随时兼容新设备）。

例如：去年发布的第一代 iPhone X 对应的 `device mode` 为 `iPhone10,3` 和 `iPhone10,6`，而今年最新发布 `iPhone XS` 对应 `iPhone11,2`，`iPhone XS Max` 对应 `iPhone11,4` 和 `iPhone11,6`，`iPhone XR` 对应 `iPhone11,8`，完整的 `device mode` 数据参考 [Wiki](https://www.theiphonewiki.com/wiki/Models)

综上，我们可以先获取 `device model` 值，记为 `platform`，然后进行对比判断，转换成具体的设备型号，实现代码如下图所示。

![](/files/-LUYjo6RN5DYFpDApsGG)

![](/files/-LUYjo6TlK00r1Gpt2oZ)

备注：图中代码只给了对 iPhone 设备型号的判断，而完整的包括 `iPad` 和 `iPod touch` 型号我已经放在 `GitHub Gist` 上，大家可以参考，详见[这里](https://gist.github.com/kangzubin/5b4f989d6b1113bfbe43c5772f3ba1fd)

## 参考链接

* [fahrulazmi/UIDeviceHardware](https://github.com/fahrulazmi/UIDeviceHardware)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://awesome-tips.gitbook.io/ios/qi-ta/content-7.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
