Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check battery level in background?

Tags:

ios

swift

ios8

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)
        }
    }
}
like image 775
captain_rillette Avatar asked May 21 '15 09:05

captain_rillette


People also ask

How do I check my exact battery level?

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.

What is background activity on iPhone battery?

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.

What does background battery usage mean?

This means the app is using battery power by running in the background.


1 Answers

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.

enter image description here

like image 164
Quentin Hayot Avatar answered Sep 19 '22 13:09

Quentin Hayot