uidevicebatterystatedidchangenotification有时不起作用 [英] uidevicebatterystatedidchangenotification not working sometimes

查看:310
本文介绍了uidevicebatterystatedidchangenotification有时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查设备的充电端口是否工作正常. 有两种情况- 一种.当充电电缆已经插入时. b.一段时间后插入充电电缆时.

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.

对于情况a,在viewDidLoad()上,我检查了启用batteryStateMonitoring并检查了电池的当前状态. -一直有效.

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

对于案例b,我尝试使用NSNotification s UIDeviceBatteryStateDidChangeNotification

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
        }
    }

NSNotification UIDeviceBatteryStateDidChangeNotification并非每次都触发.它不会每次都调用选择器方法.在插入电缆的10次中,它仅成功响应2次.即使设备开始充电.

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.

推荐答案

您应该在对象设备上进行监听

You should listen on Object device

        NotificationCenter.default.addObserver(self,selector: #selector(ViewController.monitorBatteryStatus), name: NSNotification.Name.UIDeviceBatteryStateDidChange, object: UIDevice.current)

这篇关于uidevicebatterystatedidchangenotification有时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆