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不會是什麼!非常感謝!