// // DeviceModelNameResolver.swift // suixinkan // // 将 utsname 硬件标识符映射为用户可读的设备型号名称。 // import Foundation import UIKit /// 设备硬件标识符到市场型号名称的解析器。 enum DeviceModelNameResolver { /// 返回当前设备的可读型号名称,如「iPhone X」。 static func marketingName() -> String { marketingName(forHardwareIdentifier: hardwareIdentifier()) } /// 将硬件标识符解析为可读型号名称。 static func marketingName(forHardwareIdentifier identifier: String) -> String { if let name = identifierMap[identifier] { return name } return fallbackName(for: identifier) } /// 读取当前设备硬件标识符,模拟器优先使用 SIMULATOR_MODEL_IDENTIFIER。 static func hardwareIdentifier() -> String { #if targetEnvironment(simulator) if let simulatorIdentifier = ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"], !simulatorIdentifier.isEmpty { return simulatorIdentifier } #endif var systemInfo = utsname() uname(&systemInfo) return withUnsafePointer(to: &systemInfo.machine) { $0.withMemoryRebound(to: CChar.self, capacity: 1) { String(cString: $0) } } } /// 未收录的新机型回退为「iPhone / iPad / iPod touch」,避免展示 iPhone12,1 等内部代号。 private static func fallbackName(for identifier: String) -> String { if identifier.hasPrefix("iPhone") { return "iPhone" } if identifier.hasPrefix("iPad") { return "iPad" } if identifier.hasPrefix("iPod") { return "iPod touch" } let localized = UIDevice.current.localizedModel.trimmingCharacters(in: .whitespacesAndNewlines) return localized.isEmpty ? "iPhone" : localized } /// Apple 硬件标识符与市场型号对照表。 private static let identifierMap: [String: String] = [ // iPhone "iPhone9,1": "iPhone 7", "iPhone9,3": "iPhone 7", "iPhone9,2": "iPhone 7 Plus", "iPhone9,4": "iPhone 7 Plus", "iPhone10,1": "iPhone 8", "iPhone10,4": "iPhone 8", "iPhone10,2": "iPhone 8 Plus", "iPhone10,5": "iPhone 8 Plus", "iPhone10,3": "iPhone X", "iPhone10,6": "iPhone X", "iPhone11,2": "iPhone XS", "iPhone11,4": "iPhone XS Max", "iPhone11,6": "iPhone XS Max", "iPhone11,8": "iPhone XR", "iPhone12,1": "iPhone 11", "iPhone12,3": "iPhone 11 Pro", "iPhone12,5": "iPhone 11 Pro Max", "iPhone12,8": "iPhone SE (2nd generation)", "iPhone13,1": "iPhone 12 mini", "iPhone13,2": "iPhone 12", "iPhone13,3": "iPhone 12 Pro", "iPhone13,4": "iPhone 12 Pro Max", "iPhone14,4": "iPhone 13 mini", "iPhone14,5": "iPhone 13", "iPhone14,2": "iPhone 13 Pro", "iPhone14,3": "iPhone 13 Pro Max", "iPhone14,6": "iPhone SE (3rd generation)", "iPhone14,7": "iPhone 14", "iPhone14,8": "iPhone 14 Plus", "iPhone15,2": "iPhone 14 Pro", "iPhone15,3": "iPhone 14 Pro Max", "iPhone15,4": "iPhone 15", "iPhone15,5": "iPhone 15 Plus", "iPhone16,1": "iPhone 15 Pro", "iPhone16,2": "iPhone 15 Pro Max", "iPhone17,3": "iPhone 16", "iPhone17,4": "iPhone 16 Plus", "iPhone17,1": "iPhone 16 Pro", "iPhone17,2": "iPhone 16 Pro Max", "iPhone17,5": "iPhone 16e", // iPad(少量常见型号,避免误连 iPad 时展示内部代号) "iPad13,18": "iPad (10th generation)", "iPad13,19": "iPad (10th generation)", "iPad14,1": "iPad mini (6th generation)", "iPad14,2": "iPad mini (6th generation)", "iPad13,1": "iPad Air (4th generation)", "iPad13,2": "iPad Air (4th generation)", "iPad13,16": "iPad Air (5th generation)", "iPad13,17": "iPad Air (5th generation)", "iPad8,1": "iPad Pro 11-inch", "iPad8,2": "iPad Pro 11-inch", "iPad8,3": "iPad Pro 11-inch", "iPad8,4": "iPad Pro 11-inch", "iPad8,5": "iPad Pro 12.9-inch", "iPad8,6": "iPad Pro 12.9-inch", "iPad8,7": "iPad Pro 12.9-inch", "iPad8,8": "iPad Pro 12.9-inch" ] }