分支 | 构建状态 | 版本 |
---|---|---|
master | - | |
Swift 5.0 | ≥ 1.0.0 | |
Swift 4.2 | ≥ 0.8.0 | |
低于Swift 4.2 | < 0.7.2 |
功能
- 设备识别
- 设备系列检测
- 设备型号检测
- 设备尺寸检测
- 设备组检测
- 模拟器检测
- 亮度检测
- 电池检测
- 等价性
安装
CocoaPods
Devices可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的Podfile中
pod "Devices"
使用方法
首先确保导入框架
import Devices
iOS
设备系列
简单代码
func iOSDeviceFamily() {
let family = Device.family()
switch family {
case .iPhone:
print("Device belong to \(family) family")
case .unknown:
print("unknown Device.")
}
}
示例代码: 设备系列示例代码
设备型号
简单代码
func iOSDeviceModel() {
let model = Device.model()
print(model.identifier)
switch model {
/*** iPod ***/
case .iPodTouch6Gen:
print("Device is a \(model)")
/*** iPhone ***/
case .iPhoneX:
print("Device is a \(model)")
/*** iPad ***/
case .iPadPro10_5Inch:
print("Device is a \(model)")
/*** HomePod ***/
case .HomePod:
print("Device is a \(model)")
case .simulator(.iPhoneX):
print("Device is a \(model)")
/*** unknown ***/
case .unknown:
print("unknown Device.")
}
}
示例代码: 型号示例代码
设备屏幕尺寸
简单代码
func iOSDeviceSize() {
let size = Device.size()
switch size {
case .screen12_9Inch:
print("Device size: \(size.description)")
case .unknown:
print("Device size unknown.")
}
}
示例代码: 尺寸示例代码
帮助器
模型
简单代码
func iOSModelHelper() {
let model = Device.model()
let allPhones = Device.Model.allPhones
if allPhones.contains(model) {
print("Current device belong to iPhone ")
}
let allSimulatorPhones = Device.Model.allSimulatorPhones
if allSimulatorPhones.contains(model) {
print("Current device belong to iPhone Simulator")
}
}
示例代码: 模型帮助器示例代码
尺寸
您可以使用像 >
、<
、>=
、<=
、==
、!=
这样的运算符
简单代码
func iOSSizeHelper() {
let size = Device.size()
if size > .screen4_7Inch {
print("Your device screen is larger than 4.7 inch")
}
if Device.isRetina() {
print("It's a retina display")
}
}
示例代码: 尺寸帮助器示例代码
亮度
苹果没有提供获取 tvOS 和 watchOS 静幕度的 API,如果你想更改 watchOS 设备的亮度,将无法通过。因此,亮度 API 只支持 iOS 和 OSX 平台。iOS 简单代码
func iOSBrightness() {
print("Device.brightness: \(Device.brightness)")
Device.brightness = 0.8
}
OSX 简单代码
func OSXBrightness() {
print("Device.Brightness.level: \(Device.Brightness.level)")
Device.Brightness.level = .level_5
Device.Brightness.level = .custom(0.5)
}
电池
iOS 简单代码
func iOSBattery() {
print(Device.Battery.state)
let batteryState = Device.Battery.state
switch batteryState {
case .full:
print("Your battery is happy! 😊")
case .charging(let level):
print("Your battery level: \(level)")
case .unplugged(let level):
print("Your battery level: \(level)")
}
if Device.Battery.lowPowerMode {
print("Low Power mode is enabled! 🔋")
} else {
print("Low Power mode is disabled! 😊")
}
guard batteryState < .charging(80) else {
print("Your battery is happy! 😊")
return
}
}
项目
iOS 简单代码
func iOSApplication() {
print("Build: \(Device.Application.shared.build)")
print("Version: \(Device.Application.shared.version)")
print("Name: \(Device.Application.shared.name)")
print("wholeVersion: \(Device.Application.shared.wholeVersion)")
print("firstLanuch: \(Device.Application.shared.firstLanuch)")
}
OSX
tvOS
watchOS
许可证
设备受 MIT 许可证保护。有关更多信息,请参阅 LICENSE 文件。