• iOS UIDevice设备信息


    识别设备和操作系统

    1. //获得共享设备实例
    2. open class var current: UIDevice { get }
    3. //识别设备的名称
    4. open var name: String { get } // e.g. "My iPhone"
    5. //设备类型
    6. open var model: String { get } // e.g. @"iPhone", @"iPod touch"
    7. //本地化设备类型
    8. open var localizedModel: String { get } // localized version of model
    9. //操作系统名称
    10. open var systemName: String { get } // e.g. @"iOS"
    11. //操作系统版本
    12. open var systemVersion: String { get } // e.g. @"4.0"
    13. //是否支持多任务
    14. open var isMultitaskingSupported: Bool { get }

    设备方向

    1. //返回设备的物理方向
    2. open var orientation: UIDeviceOrientation { get }
    3. //是否开启面向设备的方向通知更改
    4. open var isGeneratingDeviceOrientationNotifications: Bool { get }
    5. //开始面向设备的方向通知更改
    6. open func beginGeneratingDeviceOrientationNotifications() // nestable
    7. //结束面向设备的方向通知更改
    8. open func endGeneratingDeviceOrientationNotifications()
    1. public enum UIDeviceOrientation : Int {
    2. case unknown = 0 //设备的方向不能确定
    3. case portrait = 1 //设备竖向朝上
    4. case portraitUpsideDown = 2 //设备竖向朝下
    5. case landscapeLeft = 3 //设备横向朝左
    6. case landscapeRight = 4 //设备横向朝右
    7. case faceUp = 5 //设备平放朝上
    8. case faceDown = 6 //设备平放朝下
    9. }
    1. extension UIDeviceOrientation {
    2. public var isPortrait: Bool { get }//竖向
    3. public var isLandscape: Bool { get }//横向
    4. public var isFlat: Bool { get }//平放
    5. public var isValidInterfaceOrientation: Bool { get }
    6. }

    设备方向监测 

    1. //开启监听设备方向
    2. UIDevice.current.beginGeneratingDeviceOrientationNotifications()
    3. //注册监听设备方向事件
    4. NotificationCenter.default.addObserver(self, selector: #selector(orientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
    1. //关闭监听设备方向
    2. UIDevice.current.endGeneratingDeviceOrientationNotifications()
    3. //注销监听设备方向事件
    4. NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
    1. @objc func orientationDidChange() {
    2. //竖屏
    3. print("isPortrait", UIDevice.current.orientation.isPortrait)
    4. //横屏
    5. print("isLandscape", UIDevice.current.orientation.isLandscape)
    6. //平放
    7. print("isFlat", UIDevice.current.orientation.isFlat)
    8. //竖起
    9. print("isValidInterfaceOrientation", UIDevice.current.orientation.isValidInterfaceOrientation)
    10. }

    设备的电池状态

    1. //电量
    2. open var batteryLevel: Float { get }
    3. //电池状态
    4. open var batteryState: UIDevice.BatteryState { get }
    5. //开启电池监测
    6. open var isBatteryMonitoringEnabled: Bool

     UIDeviceBatteryState电池供电的设备状态

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

    电池电量监测

    1. //开启电量监测
    2. UIDevice.current.isBatteryMonitoringEnabled = true
    3. //注册电量监测事件
    4. NotificationCenter.default.addObserver(self, selector: #selector(batteryDidChange), name: UIDevice.batteryLevelDidChangeNotification, object: nil)
    1. @objc func batteryDidChange() {
    2. print("batteryLevel", UIDevice.current.batteryLevel)
    3. }

  • 相关阅读:
    “图片在哪”、“我是temunx”、“变成思维导图用xmindparser”gpt给出文本变字典
    面试官:说一说你的第一个Java程序是怎么跑起来的
    SQL面试常见问题总结指南
    信息学奥赛一本通:1119:矩阵交换行
    最小花费——最短路
    R语言将列表数据转化为向量数据(使用unlist函数将列表数据转化为向量数据)
    git项目管理中如何fork别人的代码以及如何拉取最新的源项目代码
    Linux 设置快捷命令
    【前端必会】不知道webpack插件? webpack插件源码分析BannerPlugin
    【快速解决】实验四 对话框 《Android程序设计》实验报告
  • 原文地址:https://blog.csdn.net/watson2017/article/details/132698533