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)
}
}
}
Open your phone's Settings app. Under "Battery," see how much charge you have left, and about how long it will last. For details, tap Battery. For a graph and list of battery use, tap Battery Usage.
Background Activity means that your battery was being used while the app was doing something in the background. Audio means that apps may play audio while running in the background. No Mobile Coverage and Low Signal means that your device is searching for a signal or being used with a low signal.
This means the app is using battery power by running in the background.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With