Check battery level in background?

Deadly 提交于 2019-12-10 15:56:04

问题


Is it possible to check battery level in swift while our application is in background?

Currently I am working on one iOS application in which user will be alerted with system notification when battery reached to 30%.

I tried and I received notification randomly, sometimes 4%, sometimes 10%, mover than ever 30%.

func applicationDidEnterBackground(application: UIApplication) {
    if(UIDevice.currentDevice().batteryLevel <= 0.30 && UIDevice.currentDevice().batteryState != UIDeviceBatteryState.Charging)
    {
        Notification.addNotificationSystem("Mettre le portable à charger", fireDate: 1, iconBadgeNumber: false)
        }
    }
}

回答1:


Your code executes the battery test only one time, when the app is going in the background.
If you want to check the battery status while in the background, your app will need to be able to execute actual code there. Which means that you will need to enable one of the Background Modes capabilities (note that Apple will reject your app if you declare to need such an ability without any feature using it).

In any case: If your app gets suspended by iOS, you won't be able to check anything.



来源:https://stackoverflow.com/questions/30370343/check-battery-level-in-background

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!