DeviceKit
Version 5.5.0
| 分支 | 版本 |
|---|---|
| 掌握 | ≥2.0 |
| Swift 4-4.2 | ≥1.3<1.13 |
| Swift 3 | ≥1.0<1.3 |
| Swift 2.3 | <1.0 |
DeviceKit是UIDevice的值替代。
有关最新功能,改进和错误修复,请参阅我们的详细ChangElog。
DeviceKit可以以各种方式安装。
pod 'DeviceKit' , '~> 5.2' pod 'DeviceKit' , '3.2' pod 'DeviceKit' , '~> 1.3' pod 'DeviceKit' , '~> 1.2.3' pod 'DeviceKit' , :git => 'https://github.com/devicekit/DeviceKit.git' , :branch => 'swift-2.3-unsupported'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 " ) ,
/// ...
] github "devicekit/DeviceKit" ~> 4.0
github "devicekit/DeviceKit" ~> 3.2
github "devicekit/DeviceKit" ~> 1.3
github "devicekit/DeviceKit" ~> 1.2.3
github "devicekit/DeviceKit" "swift-2.3-unsupported"
要手动安装它,请将DeviceKit项目拖到XCode的应用程序项目中。或通过运行将其作为git subsodule添加:
$ git submodule add https://github.com/devicekit/DeviceKit.git首先确保导入框架:
import DeviceKit这是一些用法示例。所有设备也可作为模拟器提供:
. iPhone6 => . simulator ( . iPhone6 )
. iPhone6s => . simulator ( . iPhone6s )您可以在操场上尝试这些示例。
笔记:
要在操场上尝试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不会是什么!非常感谢!