我们可以通过UIDevice.current.XXX获取到下面对应的信息。
@MainActor open class UIDevice : NSObject {
open class var current: UIDevice { get }
//设备名称(通用-关于本机-名称),例如xxx的iPhone
open var name: String { get }
//设备型号,例如iPhone,iPad
open var model: String { get }
//本地化设备型号,例如iPhone,iPad
open var localizedModel: String { get }
//系统名称,例如iOS,iPadOS
open var systemName: String { get }
//系统版本,例如17.2
open var systemVersion: String { get }
//设备方向
open var orientation: UIDeviceOrientation { get }
//应用开发商标识符
@available(iOS 6.0, *)
open var identifierForVendor: UUID? { get }
//设备方向变化时是否会发出通知
open var isGeneratingDeviceOrientationNotifications: Bool { get }
//开启设备方向变化通知
open func beginGeneratingDeviceOrientationNotifications()
//关闭设备方向变化通知
open func endGeneratingDeviceOrientationNotifications()
//电池监听是否可用,打开后,batteryState、batteryLevel两个信息才准确
@available(iOS 3.0, *)
open var isBatteryMonitoringEnabled: Bool
//电池状态,插电/未插电/充满/未知
@available(iOS 3.0, *)
open var batteryState: UIDevice.BatteryState { get }
//电池电量,0.0-1.0
@available(iOS 3.0, *)
open var batteryLevel: Float { get }
//距离感应器监听是否可用
@available(iOS 3.0, *)
open var isProximityMonitoringEnabled: Bool
//距离感应器状态
@available(iOS 3.0, *)
open var proximityState: Bool { get }
//是否支持多任务
@available(iOS 4.0, *)
open var isMultitaskingSupported: Bool { get }
//检测设备是 iPhone 还是 iPad 的方法
@available(iOS 3.2, *)
open var userInterfaceIdiom: UIUserInterfaceIdiom { get }
}
1、UIDevice.current.name
UIDevice.current.name在iOS16后无法准确获取本机名称。
参照有关iOS 获取设备名不准确的问题 UIDevice.current.name
2、IDFV - Identifier For Vendor(应用开发商标识符)
官方解释:a UUID that may be used to uniquely identify the device, same across apps from a single vendor.
翻译:“可用于唯一标识设备的UUID,同一供应商的应用程序。”
这里的Vendor就是应用开发商,通过CFBundleIdentifier(DNS反转格式)的前两部分生成。例如:com.cjh.one和com.cjh.two得到的IDFV是一样的。如果用户将属于此Vendor的所有应用卸载,则IDFV的值会被重置,即使再重装此Vendor的应用,IDFV的值也和之前不同。
适用于对内:例如分析用户在应用内的行为等。
3、isGeneratingDeviceOrientationNotifications
isGeneratingDeviceOrientationNotifications是一个属性,用于检查设备方向变化时是否会发出通知。在 iOS 设备上,如果应用支持前置摄像头,默认情况下,设备方向变化通知是关闭的。
在 Swift 中,你可以使用 UIDevice.current.isGeneratingDeviceOrientationNotifications 来检查这个属性的当前状态,使用 beginGeneratingDeviceOrientationNotifications() 和 endGeneratingDeviceOrientationNotifications() 方法来开启和关闭设备方向变化通知。
4、获取电池电量
UIDevice.current.isBatteryMonitoringEnabled = true
//获得电量(返回的是Float格式,范围是0-1,这里乘以100并且转换成整数形式)
let batterylevel = Int(UIDevice.current.batteryLevel * 100)