// // UIDevice+Extension.swift // QuickLocation // import UIKit extension UIDevice { /// 硬件机型标识,如 `iPhone17,1` static var machineIdentifier: String { var systemInfo = utsname() uname(&systemInfo) return withUnsafePointer(to: &systemInfo.machine.0) { ptr in String(cString: ptr) } } /// 可读机型名,如 `iPhone 16 Pro`;未知机型回退为 `iPhone` static var modelName: String { deviceModelMap[machineIdentifier] ?? "iPhone" } /// 与系统状态栏一致的电量百分比(四舍五入);不可用时返回 `nil` static var batteryPercent: Int? { let device = UIDevice.current if !device.isBatteryMonitoringEnabled { device.isBatteryMonitoringEnabled = true } let level = device.batteryLevel guard level >= 0 else { return nil } return Int((level * 100).rounded()) } /// 是否正在充电 / 已充满接电 static var isBatteryCharging: Bool { let device = UIDevice.current if !device.isBatteryMonitoringEnabled { device.isBatteryMonitoringEnabled = true } switch device.batteryState { case .charging, .full: return true default: return false } } private static let deviceModelMap: [String: String] = [ "iPhone10,1": "iPhone 8", "iPhone10,4": "iPhone 8", "iPhone10,2": "iPhone 8 Plus", "iPhone10,5": "iPhone 8 Plus", // iPhone X系列 "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", // iPhone11 "iPhone12,1": "iPhone 11", "iPhone12,3": "iPhone 11 Pro", "iPhone12,5": "iPhone 11 Pro Max", // iPhone12 "iPhone13,1": "iPhone 12 mini", "iPhone13,2": "iPhone 12", "iPhone13,3": "iPhone 12 Pro", "iPhone13,4": "iPhone 12 Pro Max", // iPhone13 "iPhone14,4": "iPhone 13 mini", "iPhone14,5": "iPhone 13", "iPhone14,2": "iPhone 13 Pro", "iPhone14,3": "iPhone 13 Pro Max", // iPhone14 "iPhone14,7": "iPhone 14", "iPhone14,8": "iPhone 14 Plus", "iPhone15,2": "iPhone 14 Pro", "iPhone15,3": "iPhone 14 Pro Max", // iPhone15 "iPhone15,4": "iPhone 15", "iPhone15,5": "iPhone 15 Plus", "iPhone16,1": "iPhone 15 Pro", "iPhone16,2": "iPhone 15 Pro Max", // iPhone16 "iPhone17,1": "iPhone 16 Pro", "iPhone17,2": "iPhone 16 Pro Max", "iPhone17,3": "iPhone 16", "iPhone17,4": "iPhone 16 Plus", "iPhone17,5": "iPhone 16e", // iPhone17 2025机型 "iPhone18,1": "iPhone 17 Pro", "iPhone18,2": "iPhone 17 Pro Max", "iPhone18,3": "iPhone 17", "iPhone18,4": "iPhone Air", // Simulator "i386": "Simulator", "x86_64": "Simulator", "arm64": "Simulator" ] }