获取设备容量
let rootURL = URL(fileURLWithPath: NSHomeDirectory())
let totalVolume = (try? rootURL.resourceValues(forKeys: [.volumeTotalCapacityKey]))?.volumeTotalCapacity
let availableVolume = (try? rootURL.resourceValues(forKeys: [.volumeAvailableCapacityKey]))?.volumeAvailableCapacity
获取电池电量和状态
let device = UIDevice.current
let wasBatteryMonitoringEnabled = device.isBatteryMonitoringEnabled
device.isBatteryMonitoringEnabled = true
let batteryLevel = Int(round((device.batteryLevel > 0 ? device.batteryLevel : 0) * 100))
let batteryState = UIDevice.current.batteryState
switch batteryState {
case .charging:
case .full:
case .unknown:
case .unplugged:
@unknown default:
}
device.isBatteryMonitoringEnabled = wasBatteryMonitoringEnabled
获取设备基础信息
let device = UIDevice.current
let customerName = device.name
let modelName = device.model
let uuid = device.identifierForVendor?.uuidString
let systemName = device.systemName
let systemVersion = device.systemVersion
获取设备identifier
private static var identifier: String = {
var systemInfo = utsname()
uname(&systemInfo)
let mirror = Mirror(reflecting: systemInfo.machine)
let identifier = mirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8, value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
return identifier
}()