How to get real time battery level on iOS with a 1% granularity

Viewed 29691

I'm using this function to get current battery level of device:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
UIDevice *myDevice = [UIDevice currentDevice];

[myDevice setBatteryMonitoringEnabled:YES];
double batLeft = (float)[myDevice batteryLevel]; 
NSLog(@"%f",batLeft);

but the result has a 5% granularity. Example: when the phone battery is at 88%, it only logs a value of 0.85. batteryLevel only returns values in increments of 0.05. For example: 0.85, 0.9, 0.95 and never returns values like 0.82 or 0.83.

Is there any solution to get a percentage with a higher precision?

7 Answers

Swift version to get the battery level:

UIDevice.current.isBatteryMonitoringEnabled = true
let batteryLevel = UIDevice.current.batteryLevel 

batteryLevel return 0,39; 0,40 values for me.

You can find a perfect answer here

Xcode: 11.4 Swift: 5.2

Click Here

If you are using swift 5.1 or greater this code will definitely work for you.

Step 1:- To get started, first enable the isBatteryMonitoringEnabled property of the current device, like this:-

UIDevice.current.isBatteryMonitoringEnabled = true

Step 2:- You can now read the current battery level

let level = UIDevice.current.batteryLevel
print(level)
Related