2

I'm new on stackoverflow. I've searched on so many sites for a solution to this question but I haven't found anything complete. My question is how can I obtain the percent value of the battery on an iPhone with the accuracy of 1%. I know that it's possible with a jailbroken device, but how? My code is below but it returns a value of 0.

float percent =[[objc_getClass("SBUIController")sharedInstance] displayBatteryCapacityAsPercentage];

batteryLabel.text = [NSString stringWithFormat: @"Battery is at %0.0f", percent];
Larme
  • 24,190
  • 6
  • 51
  • 81
perascotta
  • 133
  • 9
  • You can also download sample code http://mobiledevelopertips.com/device/display-battery-state-and-level-of-charge.html – Gopal Raju May 20 '14 at 14:19
  • Just so you know ... the battery percentage shown in the task bar still isn't the "perfect %" of the battery. iOS lies to you about the true battery percentage, as does almost anything that uses lithium ion batteries. The device usually doesn't let you charge up to 100%, or run it down to 0%, because that's bad for the battery's longevity. If you meant that you want to see what the status bar shows, then creker's answer is the one. – Nate May 22 '14 at 07:08

2 Answers2

5

Have a look at:

[UIDevice currentDevice].batteryMonitoringEnabled = YES;
return [NSString stringWithFormat: @"Battery is at %0.0f", [UIDevice currentDevice].batteryLevel * 100];
Adam Wright
  • 48,938
  • 12
  • 131
  • 152
  • 1
    Yes but this is the normal method and gives a value interval like 5% (40%, 45%, 50%, and so on). I would like to have the perfect value of the charge and I know that this could be the correct way to obtain that, but I always receive 0. – perascotta May 20 '14 at 14:17
  • 1
    Which is the best you can get via the public API. There are no more granular methods available. – Adam Wright May 20 '14 at 14:21
3

From libMobileGestalt.dylib

CFPropertyListRef MGCopyAnswer(CFStringRef property)

MGCopyAnswer(CFSTR("BatteryCurrentCapacity"))

Works even in AppStore app on my jailbroken phone.

UPDATE

batteryLabel.text = [NSString stringWithFormat: @"Battery is at %@", MGCopyAnswer(CFSTR("BatteryCurrentCapacity"))];
creker
  • 9,400
  • 1
  • 30
  • 47
  • And how can I use that exactly? – perascotta May 20 '14 at 16:53
  • I gave you an example. Did you even try to use it? First line is function declaration because that's a private API. Second line is how you need to call it in order to get current battery level. I added another example to match the code in the question. – creker May 20 '14 at 17:11
  • Yes man, thanks but my app (compiled by theos) still doesn't run. Should I include some headers file maybe? – perascotta May 20 '14 at 17:49
  • You just need to paste function declaration from the answer and link libMobileGestalt.dylib. That's it. If you're using that function in obj-c++ code then you need to declare it like this `extern "C" CFPropertyListRef MGCopyAnswer(CFStringRef property)`. What kind of error do you get? – creker May 20 '14 at 17:59
  • So, I've declared the function like you told me. But how can I link libMobileGestalt.dylib ? Sorry for the question but it's the first time I use this library. However, theos gives me that error `Undefined symbols for architecture armv7: "_MGCopyAnswer", referenced from: -[RootViewController rightButtonPressed] in RootViewController.mm.1df85f31.o ld: symbol(s) not found for architecture armv7 clang: error: linker command failed with exit code 1 (use -v to see invocation)` – perascotta May 20 '14 at 18:12
  • You can link it like any other framework from project's build phase "Link binary with libraries". Xcode knows about libMobileGestalt.dylib and it will be in the list. After you link it the error will go away becase that's what it tells you - you didn't link required library. – creker May 20 '14 at 18:30