iOS 设备机型判断大全更新到 iPhone 14

在开发 iOS 应用程序时,经常需要根据用户所使用的设备机型来进行一些特定的处理。因此,准确判断设备机型是非常重要的。本文将介绍一种方法,通过读取设备的硬件标识符来判断 iOS 设备的具体型号,包括最新的 iPhone 14。

1. 设备的硬件标识符

每款 iOS 设备都有一个唯一的硬件标识符,可以通过读取这个标识符来判断设备的具体型号。以 iPhone 14 为例,该设备的硬件标识符为 iPhone14,1。

2. 判断设备机型的方法

在 iOS 开发中,可以使用以下方法来判断设备机型:

import UIKit

extension UIDevice {

var modelName: String {

var systemInfo = utsname()

uname(&systemInfo)

let machine = Mirror(reflecting: systemInfo.machine).children.reduce("") { identifier, element in

guard let value = element.value as? Int8, value != 0 else { return identifier }

return identifier + String(UnicodeScalar(UInt8(value)))

}

switch machine {

case "iPod1,1": return "iPod Touch 1G"

case "iPod2,1": return "iPod Touch 2G"

case "iPod3,1": return "iPod Touch 3G"

case "iPod4,1": return "iPod Touch 4G"

case "iPod5,1": return "iPod Touch (5 Gen)"

case "iPod7,1": return "iPod Touch 6G"

case "iPod9,1": return "iPod Touch 7G"

case "iPhone1,1": return "iPhone 1G"

case "iPhone1,2": return "iPhone 3G"

case "iPhone2,1": return "iPhone 3GS"

case "iPhone3,1", "iPhone3,2", "iPhone3,3": return "iPhone 4"

case "iPhone4,1": return "iPhone 4S"

case "iPhone5,1", "iPhone5,2": return "iPhone 5"

case "iPhone5,3", "iPhone5,4": return "iPhone 5C"

case "iPhone6,1", "iPhone6,2": return "iPhone 5S"

case "iPhone7,2": return "iPhone 6"

case "iPhone7,1": return "iPhone 6 Plus"

case "iPhone8,1": return "iPhone 6S"

case "iPhone8,2": return "iPhone 6S Plus"

case "iPhone8,4": return "iPhone SE"

case "iPhone9,1", "iPhone9,3": return "iPhone 7"

case "iPhone9,2", "iPhone9,4": return "iPhone 7 Plus"

case "iPhone10,1", "iPhone10,4": return "iPhone 8"

case "iPhone10,2", "iPhone10,5": return "iPhone 8 Plus"

case "iPhone10,3", "iPhone10,6": return "iPhone X"

case "iPhone11,2": return "iPhone XS"

case "iPhone11,4", "iPhone11,6": return "iPhone XS Max"

case "iPhone11,8": return "iPhone XR"

case "iPhone12,1": return "iPhone 11"

case "iPhone12,3": return "iPhone 11 Pro"

case "iPhone12,5": return "iPhone 11 Pro Max"

case "iPhone12,8": return "iPhone SE (2 Gen)"

case "iPhone13,1": return "iPhone 12 Mini"

case "iPhone13,2": return "iPhone 12"

case "iPhone13,3": return "iPhone 12 Pro"

case "iPhone13,4": return "iPhone 12 Pro Max"

case "iPhone14,1": return "iPhone 14"

case "iPad1,1": return "iPad 1G"

case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4": return "iPad 2"

case "iPad3,1", "iPad3,2", "iPad3,3": return "iPad 3"

case "iPad3,4", "iPad3,5", "iPad3,6": return "iPad 4"

case "iPad4,1", "iPad4,2", "iPad4,3": return "iPad Air"

case "iPad5,3", "iPad5,4": return "iPad Air 2"

case "iPad6,11", "iPad6,12": return "iPad 5"

case "iPad7,5", "iPad7,6": return "iPad 6"

case "iPad11,4", "iPad11,5": return "iPad Air (3 Gen)"

case "iPad2,5", "iPad2,6", "iPad2,7": return "iPad Mini"

case "iPad4,4", "iPad4,5", "iPad4,6": return "iPad Mini 2"

case "iPad4,7", "iPad4,8", "iPad4,9": return "iPad Mini 3"

case "iPad5,1", "iPad5,2": return "iPad Mini 4"

case "iPad11,1", "iPad11,2": return "iPad Mini (5 Gen)"

case "iPad6,3", "iPad6,4": return "iPad Pro (9.7 inch)"

case "iPad6,7", "iPad6,8": return "iPad Pro (12.9 inch)"

case "iPad7,1", "iPad7,2": return "iPad Pro 2 (12.9 inch)"

case "iPad7,3", "iPad7,4": return "iPad Pro (10.5 inch)"

case "iPad8,1", "iPad8,2", "iPad8,3", "iPad8,4": return "iPad Pro (11 inch)"

case "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8": return "iPad Pro 3 (12.9 inch)"

case "iPad11,3", "iPad11,4": return "iPad Pro (11 inch) (2 Gen)"

case "iPad8,9", "iPad8,10": return "iPad Pro 4 (12.9 inch)"

case "iPad13,1", "iPad13,2": return "iPad Pro (11 inch) (3 Gen)"

case "iPad13,4", "iPad13,5", "iPad13,6", "iPad13,7": return "iPad Pro 5 (12.9 inch)"

default: return machine

}

}

}

3. 例子

以下是使用上述方法判断设备机型的示例代码:

let device = UIDevice.current

let modelName = device.modelName

print("当前设备型号为:\(modelName)")

通过上述代码,可以获取到当前设备的型号,并打印出来。

4. 总结

通过读取设备的硬件标识符,我们可以准确判断 iOS 设备的具体型号。在开发中,根据设备机型进行相应的处理,可以提供更好的用户体验。在更新到最新的 iPhone 14 后,只需在方法中添加对应的硬件标识符判断即可。

希望本文对你理解 iOS 设备机型判断有一定的帮助!如果有任何疑问或建议,请随时留言。谢谢!

参考资料:

Code by Kevin