1

I have tried to get correctly battery level. But that value is not like value in status bar of iphone.

by code use UIDevice:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

[[UIDevice currentDevice] batteryLevel];

please someone help me! I need get correctly battery level that like in status bar imdimatery.

Vladimir
  • 170,431
  • 36
  • 387
  • 313
lithewall
  • 66
  • 1
  • 1
  • 4
  • 3
    Did you actually read the documentation? http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instp/UIDevice/batteryLevel – Luke Aug 03 '12 at 19:19
  • Possible dumplicate with http://stackoverflow.com/questions/11807295/how-to-get-real-time-battery-level-on-ios – cat May 28 '13 at 02:56

2 Answers2

10

Well the Apple docs say this:

Battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged). Before accessing this property, ensure that battery monitoring is enabled.

So your code should be like this:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
float batteryLevel = [[UIDevice currentDevice] batteryLevel];

//This will give you the battery between 0.0 (empty) and 1.0 (100% charged)
//If you want it as a percentage, you can do this:

batteryLevel *= 100;

Hope this helps!

pasawaya
  • 11,515
  • 7
  • 53
  • 92
  • :( I did it. But that return 5% in earch step. 100%->95%->90%... But that not correctly like value in status bar. please help me another way. I used some soft in ios5, them get correctly value. I think we can get... but i don't how to do. Sr my english very bad – lithewall Aug 04 '12 at 02:56
  • 1
    You can't. Without a Jailbreak you can't get *exact* battery percents – Allison Mar 05 '13 at 01:09
  • I know its old but there is this app on the app-store called Battery Doctor and it is showing exact values for battery level and charging status. Any idea on how it does that? – ShayanK Mar 15 '16 at 22:50
3

Swift version to get the battery level:

UIDevice.current.isBatteryMonitoringEnabled = true
let batteryLevel = UIDevice.current.batteryLevel * 100
maxwell
  • 3,788
  • 6
  • 26
  • 40