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

29,133

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.

iPhone 5 Battery

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

Click Here

Share:
29,133
cat
Author by

cat

Updated on April 22, 2020

Comments

  • cat
    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
    cat over 11 years
    Thank 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
    simonthumper over 7 years
    Does this app remain running and reporting percentages in the background? Or do you simply leave the app running?
  • Victor Engel
    Victor Engel almost 7 years
    Your first line is superfluous since you do exactly the same thing on the third line of code.
  • PaulMest
    PaulMest over 5 years
    @VictorEngel I just edited it to remove the duplicate line of code. Thanks for pointing that out.
  • Maninder Singh
    Maninder Singh almost 5 years
    It is not working for me? Do I need to perform any other configuration? I am getting -1 as an answer.
  • maxwell
    maxwell almost 5 years
    Did you write this code? UIDevice.current.isBatteryMonitoringEnabled = true If you didn't write this you will get -1.
  • Maninder Singh
    Maninder Singh almost 5 years
    Yes, 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
    maxwell almost 5 years
    Of course, the simulator will return "-1". Use this code only for real devices.