iOS UIDevice设备信息

识别设备和操作系统

Swift 复制代码
//获得共享设备实例
open class var current: UIDevice { get }

//识别设备的名称
open var name: String { get } // e.g. "My iPhone"

//设备类型
open var model: String { get } // e.g. @"iPhone", @"iPod touch"

//本地化设备类型
open var localizedModel: String { get } // localized version of model

//操作系统名称
open var systemName: String { get } // e.g. @"iOS"

//操作系统版本
open var systemVersion: String { get } // e.g. @"4.0"

//是否支持多任务
open var isMultitaskingSupported: Bool { get }

设备方向

Swift 复制代码
//返回设备的物理方向
open var orientation: UIDeviceOrientation { get }

//是否开启面向设备的方向通知更改
open var isGeneratingDeviceOrientationNotifications: Bool { get }

//开始面向设备的方向通知更改
open func beginGeneratingDeviceOrientationNotifications() // nestable

//结束面向设备的方向通知更改
open func endGeneratingDeviceOrientationNotifications()
Swift 复制代码
public enum UIDeviceOrientation : Int {
    case unknown = 0 //设备的方向不能确定
    case portrait = 1 //设备竖向朝上
    case portraitUpsideDown = 2 //设备竖向朝下
    case landscapeLeft = 3 //设备横向朝左
    case landscapeRight = 4 //设备横向朝右
    case faceUp = 5 //设备平放朝上
    case faceDown = 6 //设备平放朝下
}
Swift 复制代码
extension UIDeviceOrientation {
    public var isPortrait: Bool { get }//竖向
    public var isLandscape: Bool { get }//横向
    public var isFlat: Bool { get }//平放
    public var isValidInterfaceOrientation: Bool { get }
}

设备方向监测

Swift 复制代码
//开启监听设备方向
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
//注册监听设备方向事件
NotificationCenter.default.addObserver(self, selector: #selector(orientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
Swift 复制代码
//关闭监听设备方向
UIDevice.current.endGeneratingDeviceOrientationNotifications()
//注销监听设备方向事件
NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
Swift 复制代码
@objc func orientationDidChange() {
    //竖屏
    print("isPortrait", UIDevice.current.orientation.isPortrait)
    //横屏
    print("isLandscape", UIDevice.current.orientation.isLandscape)
    //平放
    print("isFlat", UIDevice.current.orientation.isFlat)
    //竖起
    print("isValidInterfaceOrientation", UIDevice.current.orientation.isValidInterfaceOrientation)
}

设备的电池状态

Swift 复制代码
//电量
open var batteryLevel: Float { get }

//电池状态
open var batteryState: UIDevice.BatteryState { get }

//开启电池监测
open var isBatteryMonitoringEnabled: Bool

UIDeviceBatteryState电池供电的设备状态

Swift 复制代码
public enum BatteryState : Int {
    case unknown = 0    //设备的电池状态不能确定
    case unplugged = 1    //设备不是插入电源,电池放电
    case charging = 2    //设备插入电源和电池充电不到100%
    case full = 3        //设备插入电源和电池充电100%
}

电池电量监测

Swift 复制代码
//开启电量监测
UIDevice.current.isBatteryMonitoringEnabled = true
//注册电量监测事件
NotificationCenter.default.addObserver(self, selector: #selector(batteryDidChange), name: UIDevice.batteryLevelDidChangeNotification, object: nil)
Swift 复制代码
@objc func batteryDidChange() {
    print("batteryLevel", UIDevice.current.batteryLevel)
}
相关推荐
芝麻开门-新起点6 小时前
Android 和 iOS 系统版本及开发适配
android·ios·cocoa
2501_915918416 小时前
iOS描述文件功能解析
android·macos·ios·小程序·uni-app·cocoa·iphone
汉秋10 小时前
SwiftUI动画之使用 navigationTransition(.zoom) 实现 Hero 动画
ios·swiftui
加油乐16 小时前
解决 iOS 端输入框聚焦时页面上移问题
前端·javascript·ios
电话交换机IPPBX-3CX21 小时前
电话交换机软件 3CX iOS 应用 V5.4 Beta 更新
ios·软件更新·ip pbx·电话交换机
初遇你时动了情1 天前
uniapp/flutter中实现苹果IOS 26 毛玻璃效果、跟随滑动放大动画
flutter·ios·uni-app
2501_916007471 天前
Fastlane 结合 开心上架(Appuploader)命令行实现跨平台上传发布 iOS App 的完整方案
android·ios·小程序·https·uni-app·iphone·webview
CV大师杨某1 天前
如何在uni-app中禁用iOS橡皮筋效果?
ios·uni-app
2501_915918411 天前
iOS 上架应用市场全流程指南,App Store 审核机制、证书管理与跨平台免 Mac 上传发布方案(含开心上架实战)
android·macos·ios·小程序·uni-app·cocoa·iphone
C_philadd1 天前
Xcode26升级以后重要
ios