如何使用Swift检测充电器是否已插入或拔出?

Fra*_*cak 2 xcode swift ios9

我想检测充电器何时插上以及何时拔下插头.我想用Swift写这个.如果有人为它共享一个代码片段会很棒,但是如果你能告诉我使用它会很好.

May*_*tel 7

有关详细信息,请参阅此文档.

Swift:

UIDevice.currentDevice().setBatteryMonitoringEnabled(true)
if UIDevice.currentDevice().batteryState() == UIDeviceBatteryStateCharging {
      NSLog("Device is charging.")
}
Run Code Online (Sandbox Code Playgroud)