jsdw_ios/QuickLocation/Core/Extension/UIDevice+Extension.swift

102 lines
3.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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"
]
}