1

I am trying to check if device's charging port is working fine. There are two scenarios - a. when the charging cable is already plugged in. b. when the charging cable is plugged in after sometime.

For case a, on viewDidLoad() I checked for enable batteryStateMonitoring and checked the current state of the battery. - It always works.

For case b, I tried using NSNotifications UIDeviceBatteryStateDidChangeNotification

     override func viewDidLoad() {
       UIDevice.current.isBatteryMonitoringEnabled = true
       self.checkForCharging()
     }

func checkForCharging() {
if UIDevice.current.batteryState == .full || UIDevice.current.batteryState == .charging {
            self.updateUIForChargingTest()
        } else {
            NotificationCenter.default.addObserver(self,selector: #selector(ViewController.monitorBatteryStatus), name: NSNotification.Name.UIDeviceBatteryStateDidChange, object: nil)
        }
}

func monitorBatteryStatus() {
 if self.MonitorBatteryStatus(state: UIDevice.current.batteryState) == true {
            self.updateUIForChargingTest()
        } 
            else {
            print("Device Battery State Unknown")
        }
}

    func MonitorBatteryStatus(state : UIDeviceBatteryState) -> Bool {

        switch state {
        case UIDeviceBatteryState.charging:
            return true
        case UIDeviceBatteryState.full:
            return true
        default:
            return false
        }
    }

The NSNotification UIDeviceBatteryStateDidChangeNotification is not triggered everytime. It doesn't call the selector method everytime. Out of 10 times I plug-in the cable, it successfully responds only 2 times. Even when device begins charging.

Some people have marked it as duplicate. So for you kind information, this isnt an exact duplicate question because here I have applied certain approach to a problem which isnot working for me..AT ALL!! Solutions provided in the questions similar to my question are nt working for me, Since i have already tried them.

Akaanksha
  • 161
  • 1
  • 14
  • Just add the observer inside your viewDidLoad method after enabling it. – Leo Dabus Sep 07 '17 at 14:13
  • @LeoDabus I have added a notification in ViewDidLoad itself. i have called the method checkForCharging. It checks for the current state if device is already being charged on view load. If not, only then it triggers a notificaion. But in my case, this notification is triggered randomly. Sometimes it triggers and sometimes doesnt – Akaanksha Sep 07 '17 at 18:18
  • Edit your question and update your code – Leo Dabus Sep 07 '17 at 18:25
  • Btw I didn't say for you to call that method I said to add the observer without any conditions and do it only once. – Leo Dabus Sep 07 '17 at 18:35
  • @LeoDabus I even tried this. Just added a notification on ViewDidLoad(). But it is still not triggering event eveytime. It does it 4 out of 10 times. – Akaanksha Sep 08 '17 at 06:11
  • Edit your question and add your actual code – Leo Dabus Sep 08 '17 at 06:12
  • @LeoDabus This is my actual code that is added in my question. – Akaanksha Sep 08 '17 at 09:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153939/discussion-between-akaanksha-and-leo-dabus). – Akaanksha Sep 08 '17 at 09:46
  • Your actual code is totally different from the one I answered in the linked question. Try the code I posted if it doesn't work let me know. I am not supposed to answer the same question twice. – Leo Dabus Sep 08 '17 at 13:10

1 Answers1

2

You should listen on Object device

        NotificationCenter.default.addObserver(self,selector: #selector(ViewController.monitorBatteryStatus), name: NSNotification.Name.UIDeviceBatteryStateDidChange, object: UIDevice.current)
AppleBee
  • 1,199
  • 12
  • 26