分支 | 版本 |
---|---|
master | ≥ 2.0 |
Swift 4 - 4.2 | ≥ 1.3 < 1.13 |
Swift 3 | ≥ 1.0 < 1.3 |
Swift 2.3 | < 1.0 |
DeviceKit
是 UIDevice
的值类型替代品。
当前版本 5.0.0
请参阅我们的详细 变更日志 了解最新功能、改进和错误修复。
特性
- 可等价
- 设备识别
- 设备系列检测
- 设备组检测
- 模拟器检测
- 电池状态
- 电池电量
- 各种设备指标(例如屏幕大小、屏幕比例、PPI)
- 低电量模式检测
- 辅助访问会话检测
- 屏幕亮度
- 显示缩放检测
- 检测可用的传感器(Touch ID、Face ID)
- 检测可用磁盘空间
- Apple Pencil 支持检测
要求
- iOS 11.0+
- tvOS 11.0+
- watchOS 4.0+
安装
DeviceKit可以通过多种方式安装。
CocoaPods
Swift 5
pod 'DeviceKit', '~> 4.0'
iOS 8.0 支持
pod 'DeviceKit', '3.2'
Swift 4.0 - Swift 4.2
pod 'DeviceKit', '~> 1.3'
Swift 3
pod 'DeviceKit', '~> 1.2.3'
Swift 2.3(不支持)
pod 'DeviceKit', :git => 'https://github.com/devicekit/DeviceKit.git', :branch => 'swift-2.3-unsupported'
Swift 包管理器
Swift 5
dependencies: [
.package(url: "https://github.com/devicekit/DeviceKit.git", from: "4.0.0"),
/// ...
]
dependencies: [
.package(url: "https://github.com/devicekit/DeviceKit.git", from: "3.2.0"),
/// ...
]
Carthage
Swift 5
github "devicekit/DeviceKit" ~> 4.0
iOS 8.0 支持
github "devicekit/DeviceKit" ~> 3.2
Swift 4.0 - Swift 4.2
github "devicekit/DeviceKit" ~> 1.3
Swift 3
github "devicekit/DeviceKit" ~> 1.2.3
Swift 2.3 (不受支持)
github "devicekit/DeviceKit" "swift-2.3-unsupported"
手动操作
要手动安装它,将 Project 中的 DeviceKit
项目拖到 Xcode 的应用项目中。或者通过运行以下命令将其添加为 git 子模块:
$ git submodule add https://github.com/devicekit/DeviceKit.git
用法
首先确保导入框架
import DeviceKit
以下是使用示例。所有设备都可以作为模拟器使用
.iPhone6 => .simulator(.iPhone6)
.iPhone6s => .simulator(.iPhone6s)
您可以在 Playgrounds 中尝试这些示例。
注意
为了在 Playgrounds 中尝试 DeviceKit,请先打开
DeviceKit.xcworkspace
并选择 "DeviceKit" 作为当前方案,然后对任何模拟器构建 DeviceKit.framework。
获取运行中的设备
let device = Device.current
print(device) // prints, for example, "iPhone 6 Plus"
if device == .iPhone6Plus {
// Do something
} else {
// Do something else
}
获取设备系列
let device = Device.current
if device.isPod {
// iPods (real or simulator)
} else if device.isPhone {
// iPhone (real or simulator)
} else if device.isPad {
// iPad (real or simulator)
}
检查是否运行在模拟器上
let device = Device.current
if device.isSimulator {
// Running on one of the simulators(iPod/iPhone/iPad)
// Skip doing something irrelevant for Simulator
}
获取模拟器设备
let device = Device.current
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
}
确保设备包含在预先配置的组中
let groupOfAllowedDevices: [Device] = [.iPhone6, .iPhone6Plus, .iPhone6s, .iPhone6sPlus, .simulator(.iPhone6), .simulator(.iPhone6Plus),.simulator(.iPhone6s),.simulator(.iPhone6sPlus).simulator(.iPhone8),.simulator(.iPhone8Plus),.simulator(.iPhoneX),.simulator(.iPhoneXS),.simulator(.iPhoneXSMax),.simulator(.iPhoneXR)]
let device = Device.current
if device.isOneOf(groupOfAllowedDevices) {
// Do your action
}
获取当前电池状态
注意
要获取当前电池状态,需要将
UIDevice.current.isBatteryMonitoringEnabled
设置为true
。为了避免代码中出现问题,我们在完成后读取当前设置,并将其重置为之前的设置。
if device.batteryState == .full || device.batteryState >= .charging(75) {
print("Your battery is happy! 😊")
}
获取当前电池电平
if device.batteryLevel >= 50 {
install_iOS()
} else {
showError()
}
获取低电量模式状态
if device.batteryState.lowPowerMode {
print("Low Power mode is enabled! 🔋")
} else {
print("Low Power mode is disabled! 😊")
}
检查是否指导访问会话当前已激活
if device.isGuidedAccessSessionActive {
print("Guided Access session is currently active")
} else {
print("No Guided Access session is currently active")
}
获取屏幕亮度
if device.screenBrightness > 50 {
print("Take care of your eyes!")
}
获取可用磁盘空间
if Device.volumeAvailableCapacityForOpportunisticUsage ?? 0 > Int64(1_000_000) {
// download that nice-to-have huge file
}
if Device.volumeAvailableCapacityForImportantUsage ?? 0 > Int64(1_000) {
// download that file you really need
}
信息来源
所有型号标识均来自以下网站: https://www.theiphonewiki.com/wiki/Models 或从 Xcode 所包含的模拟器应用程序中提取。
贡献
如果您需要实现特定功能或有错误体验,请打开一个问题。如果您已经扩大了 DeviceKit 的功能并希望其他人也能使用它,请提交一个拉取请求。
贡献者
为该项目做出贡献的完整人员名单可在 此处 找到。没有您,DeviceKit 就不会是现在的样子!非常感谢!