2

I want to detect when the charger is plugged and when it becomes unplugged. I want to write this using Swift. It would be great if someone share a code snippet for it, but if you can tell me what to use that would be nice.

Pang
  • 9,564
  • 146
  • 81
  • 122

1 Answers1

10

See This doc for more info.

Swift:

UIDevice.currentDevice().setBatteryMonitoringEnabled(true)
if UIDevice.currentDevice().batteryState() == UIDeviceBatteryStateCharging {
      NSLog("Device is charging.")
}
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59