uidevicebatterystatedidchangenotification not working sometimes
Asked Answered
D

1

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.

Downbow answered 7/9, 2017 at 12:31 Comment(9)
Just add the observer inside your viewDidLoad method after enabling it.Bloodline
@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 doesntDownbow
Edit your question and update your codeBloodline
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.Bloodline
@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.Downbow
Edit your question and add your actual codeBloodline
@LeoDabus This is my actual code that is added in my question.Downbow
Let us continue this discussion in chat.Downbow
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.Bloodline
R
2

You should listen on Object device

        NotificationCenter.default.addObserver(self,selector: #selector(ViewController.monitorBatteryStatus), name: NSNotification.Name.UIDeviceBatteryStateDidChange, object: UIDevice.current)
Rebuild answered 17/2, 2018 at 8:11 Comment(1)
And one more thing you should listen notifications on main thread.Rebuild

© 2022 - 2024 — McMap. All rights reserved.