有没有办法在Swift中获得设备型号名称(iPhone 4S, iPhone 5, iPhone 5S等)?

我知道有一个名为UIDevice.currentDevice()的属性。模型,但它只返回设备类型(iPod touch, iPhone, iPad, iPhone模拟器等)。

我也知道在Objective-C中使用以下方法可以轻松完成:

#import <sys/utsname.h>

struct utsname systemInfo;
uname(&systemInfo);

NSString* deviceModel = [NSString stringWithCString:systemInfo.machine
                          encoding:NSUTF8StringEncoding];

但是我正在Swift中开发我的iPhone应用程序,所以有人可以帮助我用等效的方法在Swift中解决这个问题吗?


当前回答

我在UIDevice上做了这个“纯Swift”扩展。

如果你正在寻找一个更优雅的解决方案,你可以使用我在GitHub上发布的µ-框架DeviceKit(也可以通过CocoaPods, Carthage和Swift Package Manager获得)。

代码如下:

import UIKit

public extension UIDevice {

    static let modelName: String = {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }

        func mapToDevice(identifier: String) -> String { // swiftlint:disable:this cyclomatic_complexity
            #if os(iOS)
            switch identifier {
            case "iPod5,1":                                       return "iPod touch (5th generation)"
            case "iPod7,1":                                       return "iPod touch (6th generation)"
            case "iPod9,1":                                       return "iPod touch (7th generation)"
            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 "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 "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,4":                                    return "iPhone 13 mini"
            case "iPhone14,5":                                    return "iPhone 13"
            case "iPhone14,2":                                    return "iPhone 13 Pro"
            case "iPhone14,3":                                    return "iPhone 13 Pro Max"
            case "iPhone14,7":                                    return "iPhone 14"
            case "iPhone14,8":                                    return "iPhone 14 Plus"
            case "iPhone15,2":                                    return "iPhone 14 Pro"
            case "iPhone15,3":                                    return "iPhone 14 Pro Max"
            case "iPhone8,4":                                     return "iPhone SE"
            case "iPhone12,8":                                    return "iPhone SE (2nd generation)"
            case "iPhone14,6":                                    return "iPhone SE (3rd generation)"
            case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":      return "iPad 2"
            case "iPad3,1", "iPad3,2", "iPad3,3":                 return "iPad (3rd generation)"
            case "iPad3,4", "iPad3,5", "iPad3,6":                 return "iPad (4th generation)"
            case "iPad6,11", "iPad6,12":                          return "iPad (5th generation)"
            case "iPad7,5", "iPad7,6":                            return "iPad (6th generation)"
            case "iPad7,11", "iPad7,12":                          return "iPad (7th generation)"
            case "iPad11,6", "iPad11,7":                          return "iPad (8th generation)"
            case "iPad12,1", "iPad12,2":                          return "iPad (9th generation)"
            case "iPad13,18", "iPad13,19":                        return "iPad (10th generation)"
            case "iPad4,1", "iPad4,2", "iPad4,3":                 return "iPad Air"
            case "iPad5,3", "iPad5,4":                            return "iPad Air 2"
            case "iPad11,3", "iPad11,4":                          return "iPad Air (3rd generation)"
            case "iPad13,1", "iPad13,2":                          return "iPad Air (4th generation)"
            case "iPad13,16", "iPad13,17":                        return "iPad Air (5th generation)"
            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 (5th generation)"
            case "iPad14,1", "iPad14,2":                          return "iPad mini (6th generation)"
            case "iPad6,3", "iPad6,4":                            return "iPad Pro (9.7-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) (1st generation)"
            case "iPad8,9", "iPad8,10":                           return "iPad Pro (11-inch) (2nd generation)"
            case "iPad13,4", "iPad13,5", "iPad13,6", "iPad13,7":  return "iPad Pro (11-inch) (3rd generation)"
            case "iPad14,3", "iPad14,4":                          return "iPad Pro (11-inch) (4th generation)"
            case "iPad6,7", "iPad6,8":                            return "iPad Pro (12.9-inch) (1st generation)"
            case "iPad7,1", "iPad7,2":                            return "iPad Pro (12.9-inch) (2nd generation)"
            case "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8":      return "iPad Pro (12.9-inch) (3rd generation)"
            case "iPad8,11", "iPad8,12":                          return "iPad Pro (12.9-inch) (4th generation)"
            case "iPad13,8", "iPad13,9", "iPad13,10", "iPad13,11":return "iPad Pro (12.9-inch) (5th generation)"
            case "iPad14,5", "iPad14,6":                          return "iPad Pro (12.9-inch) (6th generation)"
            case "AppleTV5,3":                                    return "Apple TV"
            case "AppleTV6,2":                                    return "Apple TV 4K"
            case "AudioAccessory1,1":                             return "HomePod"
            case "AudioAccessory5,1":                             return "HomePod mini"
            case "i386", "x86_64", "arm64":                       return "Simulator \(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "iOS"))"
            default:                                              return identifier
            }
            #elseif os(tvOS)
            switch identifier {
            case "AppleTV5,3": return "Apple TV 4"
            case "AppleTV6,2": return "Apple TV 4K"
            case "i386", "x86_64": return "Simulator \(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "tvOS"))"
            default: return identifier
            }
            #endif
        }

        return mapToDevice(identifier: identifier)
    }()

}

你这样称呼它:

let modelName = UIDevice.modelName

对于真实的设备,它返回例如:“iPad Pro(12.9英寸)(第五代)”,对于模拟器,它返回例如。模拟式iPad Pro(12.9英寸)(第五代)

下面是模型参考:

https://theiphonewiki.com/wiki/Models https://theiphonewiki.com/wiki/BORD

其他回答

这个Swift 3.0示例将当前设备模型作为枚举常量返回(以避免与字符串字面值直接比较)。enum的原始值是一个包含人类可读的iOS设备名称的字符串。因为它是Swift,所以认可的设备列表只包括最新的机型,足以支持包含Swift的iOS版本。下面的使用示例利用了这个答案末尾的实现:

    switch UIDevice().type {
    case .iPhone5:
              print("No TouchID sensor")
    case .iPhone5S:
              fallthrough
    case .iPhone6:
              fallthrough
    case .iPhone6plus:
              fallthrough
    case .iPad_Pro9_7:
              fallthrough
    case .iPad_Pro12_9:
              fallthrough
    case .iPhone7:
              fallthrough
    case .iPhone7plus:
              print("Put your thumb on the " + 
                     UIDevice().type.rawValue + " TouchID sensor")
    case .unrecognized:
              print("Device model unrecognized");
    default:
              print(UIDevice().type.rawValue + " not supported by this app");
    }

你的应用应该保持最新的新设备发布,以及当苹果为同一设备家族添加新型号时。例如,iPhone3、1 iPhone3、2 iPhone3、4都是“iPhone 4”。避免编写不考虑新型号的代码,这样你的算法就不会意外地配置失败或对新设备做出响应。你可以参考这个维护列表的iOS设备型号#更新你的应用程序在战略时刻。

iOS includes device-independent interfaces to detect hardware capabilities and parameters such as screen size. The generalized interfaces Apple provides are usually the safest, best supported mechanisms to dynamically adapt an app's behavior to different hardware. Nevertheless, the following code can be useful for prototyping, debugging, testing, or any time code needs to target a specific device family. This technique can also be useful to describe the current device by its common/publicly recognized name.

斯威夫特3

// 1. Declare outside class definition (or in its own file).
// 2. UIKit must be included in file where this code is added.
// 3. Extends UIDevice class, thus is available anywhere in app.
//
// Usage example:
//
//    if UIDevice().type == .simulator {
//       print("You're running on the simulator... boring!")
//    } else {
//       print("Wow! Running on a \(UIDevice().type.rawValue)")
//    }
import UIKit

public enum Model : String {
    case simulator   = "simulator/sandbox",
    iPod1            = "iPod 1",
    iPod2            = "iPod 2",
    iPod3            = "iPod 3",
    iPod4            = "iPod 4",
    iPod5            = "iPod 5",
    iPad2            = "iPad 2",
    iPad3            = "iPad 3",
    iPad4            = "iPad 4",
    iPhone4          = "iPhone 4",
    iPhone4S         = "iPhone 4S",
    iPhone5          = "iPhone 5",
    iPhone5S         = "iPhone 5S",
    iPhone5C         = "iPhone 5C",
    iPadMini1        = "iPad Mini 1",
    iPadMini2        = "iPad Mini 2",
    iPadMini3        = "iPad Mini 3",
    iPadAir1         = "iPad Air 1",
    iPadAir2         = "iPad Air 2",
    iPadPro9_7       = "iPad Pro 9.7\"",
    iPadPro9_7_cell  = "iPad Pro 9.7\" cellular",
    iPadPro10_5      = "iPad Pro 10.5\"",
    iPadPro10_5_cell = "iPad Pro 10.5\" cellular",
    iPadPro12_9      = "iPad Pro 12.9\"",
    iPadPro12_9_cell = "iPad Pro 12.9\" cellular",
    iPhone6          = "iPhone 6",
    iPhone6plus      = "iPhone 6 Plus",
    iPhone6S         = "iPhone 6S",
    iPhone6Splus     = "iPhone 6S Plus",
    iPhoneSE         = "iPhone SE",
    iPhone7          = "iPhone 7",
    iPhone7plus      = "iPhone 7 Plus",
    iPhone8          = "iPhone 8",
    iPhone8plus      = "iPhone 8 Plus",
    iPhoneX          = "iPhone X",
    iPhoneXS         = "iPhone XS",
    iPhoneXSmax      = "iPhone XS Max",
    iPhoneXR         = "iPhone XR",
    iPhone11         = "iPhone 11",
    iPhone11Pro      = "iPhone 11 Pro",
    iPhone11ProMax   = "iPhone 11 Pro Max",
    unrecognized     = "?unrecognized?"
}

public extension UIDevice {
    public var type: Model {
        var systemInfo = utsname()
        uname(&systemInfo)
        let modelCode = withUnsafePointer(to: &systemInfo.machine) {
            $0.withMemoryRebound(to: CChar.self, capacity: 1) {
                ptr in String.init(validatingUTF8: ptr)

            }
        }
        var modelMap : [ String : Model ] = [
            "i386"       : .simulator,
            "x86_64"     : .simulator,
            "iPod1,1"    : .iPod1,
            "iPod2,1"    : .iPod2,
            "iPod3,1"    : .iPod3,
            "iPod4,1"    : .iPod4,
            "iPod5,1"    : .iPod5,
            "iPad2,1"    : .iPad2,
            "iPad2,2"    : .iPad2,
            "iPad2,3"    : .iPad2,
            "iPad2,4"    : .iPad2,
            "iPad2,5"    : .iPadMini1,
            "iPad2,6"    : .iPadMini1,
            "iPad2,7"    : .iPadMini1,
            "iPhone3,1"  : .iPhone4,
            "iPhone3,2"  : .iPhone4,
            "iPhone3,3"  : .iPhone4,
            "iPhone4,1"  : .iPhone4S,
            "iPhone5,1"  : .iPhone5,
            "iPhone5,2"  : .iPhone5,
            "iPhone5,3"  : .iPhone5C,
            "iPhone5,4"  : .iPhone5C,
            "iPad3,1"    : .iPad3,
            "iPad3,2"    : .iPad3,
            "iPad3,3"    : .iPad3,
            "iPad3,4"    : .iPad4,
            "iPad3,5"    : .iPad4,
            "iPad3,6"    : .iPad4,
            "iPhone6,1"  : .iPhone5S,
            "iPhone6,2"  : .iPhone5S,
            "iPad4,1"    : .iPadAir1,
            "iPad4,2"    : .iPadAir2,
            "iPad4,4"    : .iPadMini2,
            "iPad4,5"    : .iPadMini2,
            "iPad4,6"    : .iPadMini2,
            "iPad4,7"    : .iPadMini3,
            "iPad4,8"    : .iPadMini3,
            "iPad4,9"    : .iPadMini3,
            "iPad6,3"    : .iPadPro9_7,
            "iPad6,11"   : .iPadPro9_7,
            "iPad6,4"    : .iPadPro9_7_cell,
            "iPad6,12"   : .iPadPro9_7_cell,
            "iPad6,7"    : .iPadPro12_9,
            "iPad6,8"    : .iPadPro12_9_cell,
            "iPad7,3"    : .iPadPro10_5,
            "iPad7,4"    : .iPadPro10_5_cell,
            "iPhone7,1"  : .iPhone6plus,
            "iPhone7,2"  : .iPhone6,
            "iPhone8,1"  : .iPhone6S,
            "iPhone8,2"  : .iPhone6Splus,
            "iPhone8,4"  : .iPhoneSE,
            "iPhone9,1"  : .iPhone7,
            "iPhone9,2"  : .iPhone7plus,
            "iPhone9,3"  : .iPhone7,
            "iPhone9,4"  : .iPhone7plus,
            "iPhone10,1" : .iPhone8,
            "iPhone10,2" : .iPhone8plus,
            "iPhone10,3" : .iPhoneX,
            "iPhone10,6" : .iPhoneX,
            "iPhone11,2" : .iPhoneXS,
            "iPhone11,4" : .iPhoneXSmax,
            "iPhone11,6" : .iPhoneXSmax,
            "iPhone11,8" : .iPhoneXR,
            "iPhone12,1" : .iPhone11,
            "iPhone12,3" : .iPhone11Pro,
            "iPhone12,5" : .iPhone11ProMax
        ]

    if let model = modelMap[String.init(validatingUTF8: modelCode!)!] {
            return model
        }
        return Model.unrecognized
    }
}

你可以使用BDLocalizedDevicesModels框架来解析设备信息并获得名称。

然后在代码中调用UIDevice.currentDevice.productName。

这里有一个没有强制unwrap和Swift 3.0的修改:

import Foundation
import UIKit


public enum Model : String {
    case simulator = "simulator/sandbox",
    iPod1          = "iPod 1",
    iPod2          = "iPod 2",
    iPod3          = "iPod 3",
    iPod4          = "iPod 4",
    iPod5          = "iPod 5",
    iPad2          = "iPad 2",
    iPad3          = "iPad 3",
    iPad4          = "iPad 4",
    iPhone4        = "iPhone 4",
    iPhone4S       = "iPhone 4S",
    iPhone5        = "iPhone 5",
    iPhone5S       = "iPhone 5S",
    iPhone5C       = "iPhone 5C",
    iPadMini1      = "iPad Mini 1",
    iPadMini2      = "iPad Mini 2",
    iPadMini3      = "iPad Mini 3",
    iPadAir1       = "iPad Air 1",
    iPadAir2       = "iPad Air 2",
    iPhone6        = "iPhone 6",
    iPhone6plus    = "iPhone 6 Plus",
    iPhone6S       = "iPhone 6S",
    iPhone6Splus   = "iPhone 6S Plus",
    iPhoneSE       = "iPhone SE",
    iPhone7        = "iPhone 7",
    iPhone7plus    = "iPhone 7 Plus",
    unrecognized   = "?unrecognized?"
}

public extension UIDevice {
    public var type: Model {
        var systemInfo = utsname()
        uname(&systemInfo)
        let modelCode = withUnsafePointer(to: &systemInfo.machine) {
            $0.withMemoryRebound(to: CChar.self, capacity: 1) {
                ptr in String.init(validatingUTF8: ptr)

            }
        }
        var modelMap : [ String : Model ] = [
            "i386"      : .simulator,
            "x86_64"    : .simulator,
            "iPod1,1"   : .iPod1,
            "iPod2,1"   : .iPod2,
            "iPod3,1"   : .iPod3,
            "iPod4,1"   : .iPod4,
            "iPod5,1"   : .iPod5,
            "iPad2,1"   : .iPad2,
            "iPad2,2"   : .iPad2,
            "iPad2,3"   : .iPad2,
            "iPad2,4"   : .iPad2,
            "iPad2,5"   : .iPadMini1,
            "iPad2,6"   : .iPadMini1,
            "iPad2,7"   : .iPadMini1,
            "iPhone3,1" : .iPhone4,
            "iPhone3,2" : .iPhone4,
            "iPhone3,3" : .iPhone4,
            "iPhone4,1" : .iPhone4S,
            "iPhone5,1" : .iPhone5,
            "iPhone5,2" : .iPhone5,
            "iPhone5,3" : .iPhone5C,
            "iPhone5,4" : .iPhone5C,
            "iPad3,1"   : .iPad3,
            "iPad3,2"   : .iPad3,
            "iPad3,3"   : .iPad3,
            "iPad3,4"   : .iPad4,
            "iPad3,5"   : .iPad4,
            "iPad3,6"   : .iPad4,
            "iPhone6,1" : .iPhone5S,
            "iPhone6,2" : .iPhone5S,
            "iPad4,1"   : .iPadAir1,
            "iPad4,2"   : .iPadAir2,
            "iPad4,4"   : .iPadMini2,
            "iPad4,5"   : .iPadMini2,
            "iPad4,6"   : .iPadMini2,
            "iPad4,7"   : .iPadMini3,
            "iPad4,8"   : .iPadMini3,
            "iPad4,9"   : .iPadMini3,
            "iPhone7,1" : .iPhone6plus,
            "iPhone7,2" : .iPhone6,
            "iPhone8,1" : .iPhone6S,
            "iPhone8,2" : .iPhone6Splus,
            "iPhone8,4" : .iPhoneSE,
            "iPhone9,1" : .iPhone7,
            "iPhone9,2" : .iPhone7plus,
            "iPhone9,3" : .iPhone7,
            "iPhone9,4" : .iPhone7plus,
            ]

        guard let safeModelCode = modelCode else {
            return Model.unrecognized
        }

        guard let modelString = String.init(validatingUTF8: safeModelCode) else {
            return Model.unrecognized
        }

        guard let model = modelMap[modelString] else {
            return Model.unrecognized
        }

        return model
    }
}

以下是2023年1月更新的解决方案。包括最新的iPhone、iPad、Watch、iPod、Apple TV和HomePod:

extension UIDevice {
    
    static var modelName: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }

        switch identifier {
            // MARK: - iPhone
            case "i386": return "iPhone Simulator"
            case "x86_64": return "iPhone Simulator"
            case "arm64": return "iPhone Simulator"
            case "iPhone1,1": return "iPhone"
            case "iPhone1,2": return "iPhone 3G"
            case "iPhone2,1": return "iPhone 3GS"
            case "iPhone3,1": return "iPhone 4"
            case "iPhone3,2": return "iPhone 4 GSM Rev A"
            case "iPhone3,3": return "iPhone 4 CDMA"
            case "iPhone4,1": return "iPhone 4S"
            case "iPhone5,1": return "iPhone 5 (GSM)"
            case "iPhone5,2": return "iPhone 5 (GSM+CDMA)"
            case "iPhone5,3": return "iPhone 5C (GSM)"
            case "iPhone5,4": return "iPhone 5C (Global)"
            case "iPhone6,1": return "iPhone 5S (GSM)"
            case "iPhone6,2": return "iPhone 5S (Global)"
            case "iPhone7,1": return "iPhone 6 Plus"
            case "iPhone7,2": return "iPhone 6"
            case "iPhone8,1": return "iPhone 6s"
            case "iPhone8,2": return "iPhone 6s Plus"
            case "iPhone8,4": return "iPhone SE (GSM)"
            case "iPhone9,1": return "iPhone 7"
            case "iPhone9,2": return "iPhone 7 Plus"
            case "iPhone9,3": return "iPhone 7"
            case "iPhone9,4": return "iPhone 7 Plus"
            case "iPhone10,1": return "iPhone 8"
            case "iPhone10,2": return "iPhone 8 Plus"
            case "iPhone10,3": return "iPhone X Global"
            case "iPhone10,4": return "iPhone 8"
            case "iPhone10,5": return "iPhone 8 Plus"
            case "iPhone10,6": return "iPhone X GSM"
            case "iPhone11,2": return "iPhone XS"
            case "iPhone11,4": return "iPhone XS Max"
            case "iPhone11,6": return "iPhone XS Max Global"
            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 2nd 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,2": return "iPhone 13 Pro"
            case "iPhone14,3": return "iPhone 13 Pro Max"
            case "iPhone14,4": return "iPhone 13 Mini"
            case "iPhone14,5": return "iPhone 13"
            case "iPhone14,6": return "iPhone SE 3rd Gen"
            case "iPhone14,7": return "iPhone 14"
            case "iPhone14,8": return "iPhone 14 Plus"
            case "iPhone15,2": return "iPhone 14 Pro"
            case "iPhone15,3": return "iPhone 14 Pro Max"
            
            // MARK: - iPod
            case "iPod1,1": return "1st Gen iPod"
            case "iPod2,1": return "2nd Gen iPod"
            case "iPod3,1": return "3rd Gen iPod"
            case "iPod4,1": return "4th Gen iPod"
            case "iPod5,1": return "5th Gen iPod"
            case "iPod7,1": return "6th Gen iPod"
            case "iPod9,1": return "7th Gen iPod"
            
            // MARK: - iPad
            case "iPad1,1": return "iPad"
            case "iPad1,2": return "iPad 3G"
            case "iPad2,1": return "2nd Gen iPad"
            case "iPad2,2": return "2nd Gen iPad GSM"
            case "iPad2,3": return "2nd Gen iPad CDMA"
            case "iPad2,4": return "2nd Gen iPad New Revision"
            case "iPad3,1": return "3rd Gen iPad"
            case "iPad3,2": return "3rd Gen iPad CDMA"
            case "iPad3,3": return "3rd Gen iPad GSM"
            case "iPad2,5": return "iPad mini"
            case "iPad2,6": return "iPad mini GSM+LTE"
            case "iPad2,7": return "iPad mini CDMA+LTE"
            case "iPad3,4": return "4th Gen iPad"
            case "iPad3,5": return "4th Gen iPad GSM+LTE"
            case "iPad3,6": return "4th Gen iPad CDMA+LTE"
            case "iPad4,1": return "iPad Air (WiFi)"
            case "iPad4,2": return "iPad Air (GSM+CDMA)"
            case "iPad4,3": return "1st Gen iPad Air (China)"
            case "iPad4,4": return "iPad mini Retina (WiFi)"
            case "iPad4,5": return "iPad mini Retina (GSM+CDMA)"
            case "iPad4,6": return "iPad mini Retina (China)"
            case "iPad4,7": return "iPad mini 3 (WiFi)"
            case "iPad4,8": return "iPad mini 3 (GSM+CDMA)"
            case "iPad4,9": return "iPad Mini 3 (China)"
            case "iPad5,1": return "iPad mini 4 (WiFi)"
            case "iPad5,2": return "4th Gen iPad mini (WiFi+Cellular)"
            case "iPad5,3": return "iPad Air 2 (WiFi)"
            case "iPad5,4": return "iPad Air 2 (Cellular)"
            case "iPad6,3": return "iPad Pro (9.7 inch, WiFi)"
            case "iPad6,4": return "iPad Pro (9.7 inch, WiFi+LTE)"
            case "iPad6,7": return "iPad Pro (12.9 inch, WiFi)"
            case "iPad6,8": return "iPad Pro (12.9 inch, WiFi+LTE)"
            case "iPad6,11": return "iPad (2017)"
            case "iPad6,12": return "iPad (2017)"
            case "iPad7,1": return "iPad Pro 2nd Gen (WiFi)"
            case "iPad7,2": return "iPad Pro 2nd Gen (WiFi+Cellular)"
            case "iPad7,3": return "iPad Pro 10.5-inch 2nd Gen"
            case "iPad7,4": return "iPad Pro 10.5-inch 2nd Gen"
            case "iPad7,5": return "iPad 6th Gen (WiFi)"
            case "iPad7,6": return "iPad 6th Gen (WiFi+Cellular)"
            case "iPad7,11": return "iPad 7th Gen 10.2-inch (WiFi)"
            case "iPad7,12": return "iPad 7th Gen 10.2-inch (WiFi+Cellular)"
            case "iPad8,1": return "iPad Pro 11 inch 3rd Gen (WiFi)"
            case "iPad8,2": return "iPad Pro 11 inch 3rd Gen (1TB, WiFi)"
            case "iPad8,3": return "iPad Pro 11 inch 3rd Gen (WiFi+Cellular)"
            case "iPad8,4": return "iPad Pro 11 inch 3rd Gen (1TB, WiFi+Cellular)"
            case "iPad8,5": return "iPad Pro 12.9 inch 3rd Gen (WiFi)"
            case "iPad8,6": return "iPad Pro 12.9 inch 3rd Gen (1TB, WiFi)"
            case "iPad8,7": return "iPad Pro 12.9 inch 3rd Gen (WiFi+Cellular)"
            case "iPad8,8": return "iPad Pro 12.9 inch 3rd Gen (1TB, WiFi+Cellular)"
            case "iPad8,9": return "iPad Pro 11 inch 4th Gen (WiFi)"
            case "iPad8,10": return "iPad Pro 11 inch 4th Gen (WiFi+Cellular)"
            case "iPad8,11": return "iPad Pro 12.9 inch 4th Gen (WiFi)"
            case "iPad8,12": return "iPad Pro 12.9 inch 4th Gen (WiFi+Cellular)"
            case "iPad11,1": return "iPad mini 5th Gen (WiFi)"
            case "iPad11,2": return "iPad mini 5th Gen"
            case "iPad11,3": return "iPad Air 3rd Gen (WiFi)"
            case "iPad11,4": return "iPad Air 3rd Gen"
            case "iPad11,6": return "iPad 8th Gen (WiFi)"
            case "iPad11,7": return "iPad 8th Gen (WiFi+Cellular)"
            case "iPad12,1": return "iPad 9th Gen (WiFi)"
            case "iPad12,2": return "iPad 9th Gen (WiFi+Cellular)"
            case "iPad14,1": return "iPad mini 6th Gen (WiFi)"
            case "iPad14,2": return "iPad mini 6th Gen (WiFi+Cellular)"
            case "iPad13,1": return "iPad Air 4th Gen (WiFi)"
            case "iPad13,2": return "iPad Air 4th Gen (WiFi+Cellular)"
            case "iPad13,4": return "iPad Pro 11 inch 5th Gen"
            case "iPad13,5": return "iPad Pro 11 inch 5th Gen"
            case "iPad13,6": return "iPad Pro 11 inch 5th Gen"
            case "iPad13,7": return "iPad Pro 11 inch 5th Gen"
            case "iPad13,8": return "iPad Pro 12.9 inch 5th Gen"
            case "iPad13,9": return "iPad Pro 12.9 inch 5th Gen"
            case "iPad13,10": return "iPad Pro 12.9 inch 5th Gen"
            case "iPad13,11": return "iPad Pro 12.9 inch 5th Gen"
            case "iPad13,16": return "iPad Air 5th Gen (WiFi)"
            case "iPad13,17": return "iPad Air 5th Gen (WiFi+Cellular)"
            case "iPad13,18": return "iPad 10th Gen"
            case "iPad13,19": return "iPad 10th Gen"
            case "iPad14,3": return "iPad Pro 11 inch 4th Gen"
            case "iPad14,4": return "iPad Pro 11 inch 4th Gen"
            case "iPad14,5": return "iPad Pro 12.9 inch 6th Gen"
            case "iPad14,6": return "iPad Pro 12.9 inch 6th Gen"
            
            // MARK: - Watch
            case "Watch1,1": return "Apple Watch 38mm case"
            case "Watch1,2": return "Apple Watch 42mm case"
            case "Watch2,6": return "Apple Watch Series 1 38mm case"
            case "Watch2,7": return "Apple Watch Series 1 42mm case"
            case "Watch2,3": return "Apple Watch Series 2 38mm case"
            case "Watch2,4": return "Apple Watch Series 2 42mm case"
            case "Watch3,1": return "Apple Watch Series 3 38mm case (GPS+Cellular)"
            case "Watch3,2": return "Apple Watch Series 3 42mm case (GPS+Cellular)"
            case "Watch3,3": return "Apple Watch Series 3 38mm case (GPS)"
            case "Watch3,4": return "Apple Watch Series 3 42mm case (GPS)"
            case "Watch4,1": return "Apple Watch Series 4 40mm case (GPS)"
            case "Watch4,2": return "Apple Watch Series 4 44mm case (GPS)"
            case "Watch4,3": return "Apple Watch Series 4 40mm case (GPS+Cellular)"
            case "Watch4,4": return "Apple Watch Series 4 44mm case (GPS+Cellular)"
            case "Watch5,1": return "Apple Watch Series 5 40mm case (GPS)"
            case "Watch5,2": return "Apple Watch Series 5 44mm case (GPS)"
            case "Watch5,3": return "Apple Watch Series 5 40mm case (GPS+Cellular)"
            case "Watch5,4": return "Apple Watch Series 5 44mm case (GPS+Cellular)"
            case "Watch5,9": return "Apple Watch SE 40mm case (GPS)"
            case "Watch5,10": return "Apple Watch SE 44mm case (GPS)"
            case "Watch5,11": return "Apple Watch SE 40mm case (GPS+Cellular)"
            case "Watch5,12": return "Apple Watch SE 44mm case (GPS+Cellular)"
            case "Watch6,1": return "Apple Watch Series 6 40mm case (GPS)"
            case "Watch6,2": return "Apple Watch Series 6 44mm case (GPS)"
            case "Watch6,3": return "Apple Watch Series 6 40mm case (GPS+Cellular)"
            case "Watch6,4": return "Apple Watch Series 6 44mm case (GPS+Cellular)"
            case "Watch6,6": return "Apple Watch Series 7 41mm case (GPS)"
            case "Watch6,7": return "Apple Watch Series 7 45mm case (GPS)"
            case "Watch6,8": return "Apple Watch Series 7 41mm case (GPS+Cellular)"
            case "Watch6,9": return "Apple Watch Series 7 45mm case (GPS+Cellular)"
            case "Watch6,10": return "Apple Watch SE 40mm case (GPS)"
            case "Watch6,11": return "Apple Watch SE 44mm case (GPS)"
            case "Watch6,12": return "Apple Watch SE 40mm case (GPS+Cellular)"
            case "Watch6,13": return "Apple Watch SE 44mm case (GPS+Cellular)"
            case "Watch6,14": return "Apple Watch Series 8 41mm case (GPS)"
            case "Watch6,15": return "Apple Watch Series 8 45mm case (GPS)"
            case "Watch6,16": return "Apple Watch Series 8 41mm case (GPS+Cellular)"
            case "Watch6,17": return "Apple Watch Series 8 45mm case (GPS+Cellular)"
            case "Watch6,18": return "Apple Watch Ultra"
            
            // MARK: - Apple TV
            case "AppleTV5,3": return "Apple TV"
            case "AppleTV6,2": return "Apple TV 4K"
            
            // MARK: - HomePod
            case "AudioAccessory1,1": return "HomePod"
            case "AudioAccessory5,1": return "HomePod mini"
            
            // MARK: - Unrecognized
            default: return identifier
        }
    }
}

用法:

UIDevice.modelName

对于设备和模拟器, 创建一个名为UIDevice.swift的新swift文件

添加以下代码

import UIKit


public extension UIDevice {

var modelName: String {
    #if (arch(i386) || arch(x86_64)) && os(iOS)
        let DEVICE_IS_SIMULATOR = true
    #else
        let DEVICE_IS_SIMULATOR = false
    #endif

    var machineString : String = ""

    if DEVICE_IS_SIMULATOR == true
    {

        if let dir = NSProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] {
            machineString = dir
        }
    }
    else {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        machineString = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8 where value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
    }
    switch machineString {
    case "iPod5,1":                                 return "iPod Touch 5"
    case "iPod7,1":                                 return "iPod Touch 6"
    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 "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 "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 "iPad6,7", "iPad6,8":                      return "iPad Pro"
    case "AppleTV5,3":                              return "Apple TV"
    default:                                        return machineString
    }
}
}

然后在你的视图控制器中,

 let deviceType = UIDevice.currentDevice().modelName

    if deviceType.lowercaseString.rangeOfString("iphone 4") != nil {
       print("iPhone 4 or iphone 4s")
    }
    else if deviceType.lowercaseString.rangeOfString("iphone 5") != nil {
        print("iPhone 5 or iphone 5s or iphone 5c")
    }
   else if deviceType.lowercaseString.rangeOfString("iphone 6") != nil {
        print("iPhone 6 Series")
    }