有没有办法在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中解决这个问题吗?


当前回答

以下是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

其他回答

我的简单解决方案是按设备分组,支持新设备iPhone 8和iPhone X在Swift 3:

public extension UIDevice {
    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 {
        case "iPhone3,1", "iPhone3,2", "iPhone3,3", "iPhone4,1":
            return "iPhone 4"

        case "iPhone5,1", "iPhone5,2", "iPhone5,3", "iPhone5,4", "iPhone6,1", "iPhone6,2", "iPhone8,4":
            return "iPhone 5"

        case "iPhone7,2", "iPhone8,1", "iPhone9,1", "iPhone9,3", "iPhone10,1", "iPhone10,4":
            return "iPhone 6,7,8"

        case "iPhone7,1", "iPhone8,2", "iPhone9,2", "iPhone9,4", "iPhone10,2", "iPhone10,5":
            return "iPhone Plus"

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

        case "i386", "x86_64":
            return "Simulator"
        default:
            return identifier
        }
    }
}

和使用:

switch UIDevice.current.modelName {
  case "iPhone 4":
  case "iPhone 5":
  case "iPhone 6,7,8":
  case "iPhone Plus":
  case "iPhone X":
  case "Simulator":
  default:
}

斯威夫特5

/// Obtain the machine hardware platform from the `uname()` unix command
///
/// Example of return values
///  - `"iPhone8,1"` = iPhone 6s
///  - `"iPad6,7"` = iPad Pro (12.9-inch)
static var unameMachine: String {
    var utsnameInstance = utsname()
    uname(&utsnameInstance)
    let optionalString: String? = withUnsafePointer(to: &utsnameInstance.machine) {
        $0.withMemoryRebound(to: CChar.self, capacity: 1) {
            ptr in String.init(validatingUTF8: ptr)
        }
    }
    return optionalString ?? "N/A"
}

这是一个用于检测apple设备的新库

import DeviceDetector

let detector = DeviceDetector.shared
let deviceName = detector.currentDeviceName
let deviceSet = detector.currentDevice
        
let information = """
Model: \(deviceName)
iPhone?: \(detector.isiPhone)
iPad?: \(detector.isiPad)
Notch?: \(detector.hasSafeArea)
        
4inch?: \(DeviceSet.iPhone4inchSet.contains(deviceSet))
4.7inch?: \(DeviceSet.iPhone4_7inchSet.contains(deviceSet))
iPhoneSE?: \(DeviceSet.iPhoneSESet.contains(deviceSet))
iPhonePlus?: \(DeviceSet.iPhonePlusSet.contains(deviceSet))
iPadPro?: \(DeviceSet.iPadProSet.contains(deviceSet))
"""

结果

下面是获取硬件字符串的代码,但是您需要比较这些硬件字符串才能知道它是哪个设备。我已经创建了一个类,包含几乎所有的设备字符串(我们保持字符串最新的新设备)。使用方便,请检查

吉hub /恶魔大师

Objective-C: GitHub/DeviceUtil

public func hardwareString() -> String {
  var name: [Int32] = [CTL_HW, HW_MACHINE]
  var size: Int = 2
  sysctl(&name, 2, nil, &size, &name, 0)
  var hw_machine = [CChar](count: Int(size), repeatedValue: 0)
  sysctl(&name, 2, &hw_machine, &size, &name, 0)

  let hardware: String = String.fromCString(hw_machine)!
  return hardware
}

在swift中处理c结构体是很痛苦的。尤其是当里面有c数组的时候。以下是我的解决方案:继续使用objective-c。只需创建一个包装器objective-c类来完成这项工作,然后在swift中使用该类。下面是一个示例类,它就是这样做的:

@interface DeviceInfo : NSObject

+ (NSString *)model;

@end

#import "DeviceInfo.h"
#import <sys/utsname.h>

@implementation DeviceInfo

+ (NSString *)model
{
    struct utsname systemInfo;
    uname(&systemInfo);

    return [NSString stringWithCString: systemInfo.machine encoding: NSUTF8StringEncoding];
}

@end

在迅捷的一面:

let deviceModel = DeviceInfo.model()