变更记录

序号 录入时间 录入人 备注
1 2016-10-14 Alfred Jiang -

方案名称

UIDevice - 通过 DeviceKit 更方便的获取硬件设备参数

关键字

UIDevice \ DeviceKit \ 设备 \ 模拟器 \ 电量信息与状态

需求场景

  1. 需要确认设备型号时
  2. 需要确认电量信息状态时

参考链接

  1. GitHub - dennisweissmann/DeviceKit(推荐)

详细内容

Usage

Here are some usage examples. All devices are also available as simulators:

1
2
.iPhone6 => .Simulator(.iPhone6)
.iPhone6s => .Simualtor(.iPhone6s)

etc.

Get the Device You’re Running On
1
2
3
4
5
6
7
8
9
let device = Device()

print(device) // prints, for example, "iPhone 6 Plus"

if device == .iPhone6Plus {
// Do something
} else {
// Do something else
}
Get the Device Family
1
2
3
4
5
6
7
8
let device = Device()
if device.isPod {
// iPods (real or simulator)
} else if device.isPhone {
// iPhone (real or simulator)
} else if device.isPad {
// iPad (real or simulator)
}
To check if running on Simulator
1
2
3
4
5
let device = Device()
if device.isSimulator {
// Running on one of the simulators(iPod/iPhone/iPad)
// Skip doing something irrelevant for Simulator
}
Get the Simulator Device
1
2
3
4
5
6
let device = Device()
switch device {
case .Simulator(.iPhone6s): break // You're running on the iPhone 6s simulator
case .Simulator(.iPadAir2): break // You're running on the iPad Air 2 simulator
default: break
}
Make Sure the Device Is Contained in a Preconfigured Group
1
2
3
4
5
6
let groupOfAllowedDevices: [Device] = [.iPhone6, .iPhone6Plus, .iPhone6s, .iPhone6sPlus, .Simulator(.iPhone6), .Simulator(.iPhone6Plus), .Simulator(.iPhone6s), .Simulator(.iPhone6sPlus)]
let device = Device()

if device.isOneOf(groupOfAllowedDevices) {
// Do you action
}
Get the Current Battery State
1
2
3
if device.batteryState == .Full || device.batteryState >= .Charging(75) {
print("Your battery is happy! 😊")
}
Get the Current Battery Level
1
2
3
4
5
if device.batteryLevel >= 50 {
install_iOS()
} else {
showError()
}

效果图

(无)

备注

(无)