1

When I am connecting the device then i can detect, that cable is connected, but when cable unplug that time how will notify that cable is unpluged.

if (UIDevice.current.batteryState == UIDeviceBatteryState.charging) 

{} else { } while unpluged method didn't call help me.

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Rahul
  • 15
  • 5

1 Answers1

0
         UIDevice.current.isBatteryMonitoringEnabled = true
         if UIDevice.current.batteryState == .charging {
              print("Device is charging.")
         } 
Mahesh Dangar
  • 806
  • 5
  • 11
  • @IBAction func swichButtonChangedStatus (mySwitch : UISwitch) { guard mySwitch.isOn else { print("Switch is off") return } if (UIDevice.current.batteryState == UIDeviceBatteryState.charging) { UIDevice.current.isBatteryMonitoringEnabled = true NotificationCenter.default.addObserver(self, selector: #selector(detectBatteryChargerUnpluged), name: .UIDeviceBatteryStateDidChange, object: nil) }else { print("Please Plug Device.") } } – Rahul May 25 '18 at 10:40