18

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?

RK-
  • 12,099
  • 23
  • 89
  • 155
cat
  • 357
  • 1
  • 5
  • 14
  • 1
    For the record: I just check your code with the iPhone 6 Plus and it does show all scalar levels, like 0.49 etc – brainray Aug 09 '15 at 20:17

7 Answers7

33

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

nst
  • 3,862
  • 1
  • 31
  • 40
  • 1
    Does this app remain running and reporting percentages in the background? Or do you simply leave the app running? – simonthumper Nov 25 '16 at 09:17
10

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.

bitmapdata.com
  • 9,572
  • 5
  • 35
  • 43
  • 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? – cat Aug 06 '12 at 01:51
4
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;
PaulMest
  • 12,925
  • 7
  • 53
  • 50
user1617027
  • 155
  • 1
  • 1
3

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.

maxwell
  • 3,788
  • 6
  • 26
  • 40
  • It is not working for me? Do I need to perform any other configuration? I am getting -1 as an answer. – Maninder Singh May 30 '19 at 14:02
  • Did you write this code? UIDevice.current.isBatteryMonitoringEnabled = true If you didn't write this you will get -1. – maxwell May 30 '19 at 15:06
  • 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? – Maninder Singh May 30 '19 at 15:13
  • 1
    Of course, the simulator will return "-1". Use this code only for real devices. – maxwell May 31 '19 at 07:51
2

The answers above are very good, but they are all in Obj-C, I have used these with other examples to do the same task on MonoTouch, so I am putting my code here in case anybody needs it:

try
{
    UIDevice.CurrentDevice.BatteryMonitoringEnabled = true;
    _Battery.Level = (int)(UIDevice.CurrentDevice.BatteryLevel * IOSBatteryLevelScalingFactor);
    _Battery.State = UIDevice.CurrentDevice.BatteryState;
}
catch (Exception e)
{
    ExceptionHandler.HandleException(e, "BatteryState.Update");
    throw new BatteryUpdateException();
}
finally
{
    UIDevice.CurrentDevice.BatteryMonitoringEnabled = false;
}

I also have a full post on my blog to give all the details in here

Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37
1

You can find a perfect answer here

Xcode: 11.4 Swift: 5.2

Click Here

0

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)
Navdeep Paliwal
  • 349
  • 3
  • 7