How to get real time battery level on iOS with a 1% granularity
Solution 1
check out this site : Reading the battery level programmatically
but, carefully use. all of the APIs used here are undocumented on the iPhone, and will probably lead to a rejection if you submit this application to the App Store. Although battery charge status is not exactly, I'd recommend using the UIDevice battery monitoring methods.
Solution 2
There are at least four different ways to read the battery level, and all four ways may return different values.
Here is a chart of these values through time.
The values were recorded with this iOS project: https://github.com/nst/BatteryChart
Please check out the code for reference.
Solution 3
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
double batLeft = (float)[myDevice batteryLevel] * 100;
NSLog(@"%.f", batLeft);
NSString * levelLabel = [NSString stringWithFormat:@"%.f%%", batLeft];
lblLevel.text = levelLabel;
Solution 4
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.
Solution 5
You can find a perfect answer here
Xcode: 11.4 Swift: 5.2
cat
Updated on April 22, 2020Comments
-
cat about 4 years
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?
-
cat over 11 yearsThank you for your link. I've checked out it. I saw this line: "Don't forget to remove the headers and libIOKit.A.dylib from your code before shipping!", did it mean after done, I can remove libIOKit.A.dylib and remove the headers from my code to upload to Apple Store?
-
simonthumper over 7 yearsDoes this app remain running and reporting percentages in the background? Or do you simply leave the app running?
-
Victor Engel almost 7 yearsYour first line is superfluous since you do exactly the same thing on the third line of code.
-
PaulMest over 5 years@VictorEngel I just edited it to remove the duplicate line of code. Thanks for pointing that out.
-
Maninder Singh almost 5 yearsIt is not working for me? Do I need to perform any other configuration? I am getting -1 as an answer.
-
maxwell almost 5 yearsDid you write this code? UIDevice.current.isBatteryMonitoringEnabled = true If you didn't write this you will get -1.
-
Maninder Singh almost 5 yearsYes, I wrote this line "UIDevice.current.isBatteryMonitoringEnabled = true". Also, I am new to developing iOS Apps. I am calling this code form viewDIdLoad() method under ViewController. Also, I am connected to a simulator, not an actual phone. Will that make a difference?
-
maxwell almost 5 yearsOf course, the simulator will return "-1". Use this code only for real devices.