这篇文章是基于 UIDevice 的 API进行分析的。最后给出了 UIDevice 的使用方法,包括能够访问设备名称,系统版本,UUID,监测电量变化,电池电量状态,监测屏幕方向变化,以及监测iPhone 是否在接近用户的身体。git
UIDevice 是 UIKit 框架下Core App 中的设备环境的一个类。用来表示当前的设备,能够使用 UIDevice 实例作什么事情呢? 具体总结以下: 1.使用UIDevice对象获取有关设备的信息,如名称,设备型号以及操做系统名称和版本。 2.能够使用UIDevice实例来检测设备特性的变化,例如物理方向。 3.能够使用orientation属性获取当前方向,并能够在设备方向改变的时候使用通知来做出相应的处理。 4.能够使用UIDevice实例获取有关电池充电状态和充电水平更改的信息和通知。 5.还提供对接近传感器状态的访问。接近传感器检测用户是否将设备保持靠近他们的脸部。仅在须要时才启用电池监控或接近感应。 6.还能够使用实例方法在自定义输入和键盘附件视图中播放键盘输入点击。github
import Foundation
import UIKit
// 设备方向。一个枚举
public enum UIDeviceOrientation : Int {
case unknown
case portrait // 竖直屏,home 键在下边
case portraitUpsideDown // 竖直屏,Home 键在上边
case landscapeLeft // 横屏,Home 在右边
case landscapeRight // 横屏,home 在左边
case faceUp // 平放,正面朝上
case faceDown // 平放,正面朝下
}
// 设备方向的扩展。 用于判断方向,只有 get 属性
extension UIDeviceOrientation {
// 是不是横屏
public var isLandscape: Bool { get }
// 是不是竖屏
public var isPortrait: Bool { get }
// 是否平放
public var isFlat: Bool { get }
// 有效的界面方向
public var isValidInterfaceOrientation: Bool { get }
}
// 电池状态枚举
public enum UIDeviceBatteryState : Int {
case unknown
case unplugged // 放电
case charging // 充电,少于100%
case full // 充满
}
// available in iPhone 3.0
// 当前设备的界面类型 枚举
public enum UIUserInterfaceIdiom : Int {
case unspecified // 未知。未指明
@available(iOS 3.2, *)
case phone // iPhone and iPod touch 风格的UI
@available(iOS 3.2, *)
case pad // iPad UI
@available(iOS 9.0, *)
case tv // Apple TV style UI
@available(iOS 9.0, *)
case carPlay // CarPlay style UI ,车载屏
}
// 方法。设备是不是竖屏,返回 Bool 值
public func UIDeviceOrientationIsPortrait(_ orientation: UIDeviceOrientation) -> Bool
// 设备是不是横屏,返回 Bool 值
public func UIDeviceOrientationIsLandscape(_ orientation: UIDeviceOrientation) -> Bool
// UIDevice类
@available(iOS 2.0, *)
open class UIDevice : NSObject {
//类属性,获取当前设备实例
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 orientation: UIDeviceOrientation { get }
//实例属性,设备的 UUID
@available(iOS 6.0, *)
open var identifierForVendor: UUID? { get }
//实例属性,一个布尔值,指示接收器是否生成方向通知(true)或不指定(false)
open var isGeneratingDeviceOrientationNotifications: Bool { get }
// 开始设备方向更改通知的生成。
open func beginGeneratingDeviceOrientationNotifications() // nestable
// 结束设备方向更改通知的生成。
open func endGeneratingDeviceOrientationNotifications()
// 指示是否启用电池监控(true)或(否false)
@available(iOS 3.0, *)
open var isBatteryMonitoringEnabled: Bool // default is NO
// 电池的状态
@available(iOS 3.0, *)
open var batteryState: UIDeviceBatteryState { get } // UIDeviceBatteryStateUnknown if monitoring disabled
// 电量 是个小数,用百分比展现
@available(iOS 3.0, *)
open var batteryLevel: Float { get } // 0 .. 1.0. 若是电池状态未知,为-1
//表示是否启用接近用户监测(true)或不(false)。
@available(iOS 3.0, *)
open var isProximityMonitoringEnabled: Bool // default is NO
// 表示接近传感器是否接近user(true)或not(false)
@available(iOS 3.0, *)
open var proximityState: Bool { get } // always returns NO if no proximity detector
// 设备是否支持接近用户监测
@available(iOS 4.0, *)
open var isMultitaskingSupported: Bool { get }
// 用户在当前设备上使用的界面风格,是上边的一个枚举
@available(iOS 3.2, *)
open var userInterfaceIdiom: UIUserInterfaceIdiom { get }
// 用来播放可用的键盘输入点击声,只有用户容许输入声音是才有用
@available(iOS 4.2, *)
open func playInputClick() // Plays a click only if an enabling input view is on-screen and user has enabled input clicks.
}
// 协议,输入视图的按键声音
public protocol UIInputViewAudioFeedback : NSObjectProtocol {
// 获取设备是否开启键盘输入声音,若是是 YES 输入是将会播放声音
optional public var enableInputClicksWhenVisible: Bool { get } // If YES, an input view will enable playInputClick.
}
// 能够用来监听的一些状态。用于通知的发送,设备状态改变时的监听。
public func UI_USER_INTERFACE_IDIOM() -> UIUserInterfaceIdiom
extension NSNotification.Name {
// 设备方向改变
public static let UIDeviceOrientationDidChange: NSNotification.Name
// 电池状态改变
@available(iOS 3.0, *)
public static let UIDeviceBatteryStateDidChange: NSNotification.Name
// 电量改变
@available(iOS 3.0, *)
public static let UIDeviceBatteryLevelDidChange: NSNotification.Name
// 设备接近状态改变
@available(iOS 3.0, *)
public static let UIDeviceProximityStateDidChange: NSNotification.Name
}
复制代码
================华丽丽分割线,代码来了===============swift
// ViewController.swift
// DeviceOrientation
// Created by YHY on 2017/3/22.
// Copyright © 2017年 太阳在线. All rights reserved.
import UIKit
class ViewController: UIViewController {
var deviceInfo: [String]!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var model: UILabel!
@IBOutlet weak var localizedModel: UILabel!
@IBOutlet weak var systemName: UILabel!
@IBOutlet weak var systemVersion: UILabel!
@IBOutlet weak var orientation: UILabel!
@IBOutlet weak var batteryState: UILabel!
@IBOutlet weak var batteryLevel: UILabel!
@IBOutlet weak var proximityState: UILabel!
@IBOutlet weak var userInterfaceIdiom: UILabel!
@IBOutlet weak var multitaskingSupported: UILabel!
@IBOutlet weak var identifierForVendor: UILabel!
@IBOutlet weak var proximity: UILabel!
var i = 0,j = 0
override func viewDidLoad() {
super.viewDidLoad()
let device = UIDevice.current
// 启用电池监控,启用以后有关电池的监测才能够用
device.isBatteryMonitoringEnabled = true
// 启用用户接近
device.isProximityMonitoringEnabled = true
print("设备名:" + device.name)
print("设备型号:" + device.model)
print("设备定位型号:" + device.localizedModel)
print("设备系统:" + device.systemName)
print("系统版本:" + device.systemVersion)
print("UUID:" + String(describing: device.identifierForVendor) )
print("电量:\(UIDevice.current.batteryLevel * 100)%")
print("是否支持接近用户监测:" + String(device.isMultitaskingSupported))
name.text = "设备名:" + device.name
model.text = "设备型号:" + device.model
localizedModel.text = "设备定位型号:" + device.localizedModel
systemName.text = "设备系统:" + device.systemName
systemVersion.text = "系统版本:" + device.systemVersion
identifierForVendor.text = "UUID:" + String(describing: device.identifierForVendor!)
batteryLevel.text = "电量:" + String(device.batteryLevel)
multitaskingSupported.text = "是否支持接近用户监测:" + String(device.isMultitaskingSupported)
switch device.userInterfaceIdiom {
case .carPlay:
print("用户界面风格:车载屏")
userInterfaceIdiom.text = "用户界面风格:车载屏"
case .pad:
print("用户界面风格:iPad")
userInterfaceIdiom.text = "用户界面风格:iPad"
case .phone:
print("用户界面风格:iPhone")
userInterfaceIdiom.text = "用户界面风格:iPhone"
case.tv:
print("用户界面风格: TV")
userInterfaceIdiom.text = "用户界面风格: TV"
default:
print("用户界面风格:未知")
userInterfaceIdiom.text = "用户界面风格:未知"
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// 监听设备方向变化
NotificationCenter.default.addObserver(self, selector: #selector(change), name: .UIDeviceOrientationDidChange, object: nil)
// 监听电池状态
NotificationCenter.default.addObserver(self, selector: #selector(battery), name: .UIDeviceBatteryStateDidChange, object: nil)
// 监听电量
NotificationCenter.default.addObserver(self, selector: #selector(batteryLevels), name: .UIDeviceBatteryLevelDidChange, object: nil)
// 监听设备是否接近用户
NotificationCenter.default.addObserver(self, selector: #selector(proximityStates), name: .UIDeviceProximityStateDidChange, object: nil)
}
func change() {
let orienta = UIDevice.current.orientation
print(orienta)
switch orienta {
case .faceDown:
orientation.text = "设备方向:脸朝地"
case .faceUp:
orientation.text = "设备方向:脸朝上"
case .landscapeLeft:
orientation.text = "设备方向:头朝左"
case .landscapeRight:
orientation.text = "设备方向:头朝右"
case .portrait:
orientation.text = "设备方向:正立"
case .portraitUpsideDown:
orientation.text = "设备方向:倒立"
default:
orientation.text = "设备方向:还在懵逼"
}
}
func battery() {
let batteryStatu = UIDevice.current.batteryState
switch batteryStatu {
case .charging:
print("正在充电")
batteryState.text = "电池状态:正在充电"
case .full:
print("满电量")
batteryState.text = "电池状态:满电量"
case .unplugged:
print("放电")
batteryState.text = "电池状态:在放电"
default:
print("我也不知道在干吗")
batteryState.text = "电池状态:我也不知道在干吗"
}
}
func batteryLevels() {
print("电量",UIDevice.current.batteryLevel)
batteryLevel.text = "通知电量变为:\(UIDevice.current.batteryLevel * 100)%"
}
func proximityStates() {
print("是否接近用户:",UIDevice.current.proximityState)
if UIDevice.current.proximityState {
i += 1
proximity.text = "接近了用户\(i)次"
}else {
j += 1
proximityState.text = "离开了\(j)次"
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
UIDevice.current.endGeneratingDeviceOrientationNotifications()
NotificationCenter.default.removeObserver(self)
}
}
复制代码
Demo 下载bash