所以,我正在试图弄清楚如何监控iOS设备中的电池电量和状态变化.
到目前为止,我已经确定了如何获得当前的电池电量,但无法弄清楚如何获得状态或如何监控任何更改,所以我可以弹出一个对话框(或通知,但我想我无法监控这个在背景中无论如何......)当100%充电时.
这是我到目前为止:
@IBOutlet var BatteryLevelLabel: UILabel!
@IBOutlet var BatteryStateLabel: UILabel!
// function to return the devices battery level
func batteryLevel()-> Float {
return UIDevice.currentDevice().batteryLevel
}
// function to return the devices battery state (Unknown, Unplugged, Charging, or Full)
func batteryState()-> UIDeviceBatteryState {
return UIDevice.currentDevice().batteryState
}
override func viewDidLoad() {
super.viewDidLoad()
let currentBatteryLevel = batteryLevel()
// enables the tracking of the devices battery level
UIDevice.currentDevice().batteryMonitoringEnabled = true
// shows the battery level on labels
BatteryLevelLabel.text = "\(batteryLevel() * 100)%)"
BatteryStateLabel.text …Run Code Online (Sandbox Code Playgroud) 我想知道 iPhone 是否以编程方式连接到 USB。
到目前为止,我通过谷歌搜索找到了http://developer.apple.com/programs/mfi/。
我真的需要这个 api 来确定 iPhone 是否连接到 USB 设备吗?
我只想制作一个 iPhone 应用程序,无论 iPhone 是否连接 USB,它都会在控制台中打印出来。
非常感谢。
我想检测充电器何时插上以及何时拔下插头.我想用Swift写这个.如果有人为它共享一个代码片段会很棒,但是如果你能告诉我使用它会很好.