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.
Asked
Active
Viewed 2,998 times
2
-
The duplicate answer given above is in objectives not Swift. – WaterNotWords Mar 09 '16 at 07:29
1 Answers
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