2

I want to show battery usage of the app on the status bar of the app. It uses GPS, so GPS signal and details of battery usage by app would be useful for the users about the battery drain.

Is there a way to get the battery usage from the device? and to show only the amount of battery drained by my app alone. On instruments it shows the graph over the time.

I want to show battery usage in numerical format, not a graph.

Monolo
  • 18,205
  • 17
  • 69
  • 103
Manoj
  • 1,482
  • 2
  • 20
  • 53
  • 1
    in a GPS-related app, I would be scared if I saw how much battery it drains :) nice idea though – Bartu May 06 '13 at 10:39

2 Answers2

4

Use below code to get battery level

UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batteryLevel = [myDevice batteryLevel];
_battery.text = [NSString stringWithFormat:@"%f",batteryLevel*100];
[myDevice batteryLevel];

will give you the battery between 0.0 (empty) and 1.0 (100% charged)

iOS - Issue with displaying battery status

Community
  • 1
  • 1
Ahmed Z.
  • 2,329
  • 23
  • 52
  • Thanks for you reply but this shows the current battery level but I wanna show amount of battery just MY APP alone drained. Sorry if my earlier question was not clear about what am looking for..... – Manoj May 06 '13 at 11:37
  • I changed my question to what am exactly I need. Thank U so much for answering my first query. Now I need to calculate on the battery usage of the app alone. Is there a way to do that? – Manoj May 06 '13 at 11:41
  • 1
    At the time of app start get the battery status and store it then in your app, after some intervals take the battery status, subtract it from the starting status and display to the user. this way you can show updated battery status to the user continuously. – Ahmed Z. May 06 '13 at 11:42
  • the difference of the battery would show how much your app has consumed the battery. i hope you got my point. – Ahmed Z. May 06 '13 at 11:44
  • This is what I was thinking but the lock here is, we can't be sure that only our app is running on the user device. What if heavy battery draining games running simultaneously with our app. We then end up showing battery drained by both (or few more running) apps. This is now not gonna show our app battery usage alone right? – Manoj May 06 '13 at 12:04
  • buddy you cant run 2 apps at once on an iphone :) – Ahmed Z. May 06 '13 at 12:07
  • This is for iOS 5 and above and so we can run multiple apps simultaneously. – Manoj May 06 '13 at 12:25
  • This is for iOS 5 and above and so we can run multiple apps simultaneously. Wherein when one app is on foreground. Other apps can run in the background – Manoj May 06 '13 at 12:33
  • the apps in background dont take that much battery. so as for your app when its in foreground, it is the major utilizer of the battery. – Ahmed Z. May 06 '13 at 12:48
0

From command line we can get the battery details of iOS phones using libimobiledevice tools.

Inside the tools use the command:

./ideviceinfo -q com.apple.mobile.battery

It will return something like:

BatteryCurrentCapacity: 30 BatteryIsCharging: true ExternalChargeCapable: true ExternalConnected: true FullyCharged: false GasGaugeCapability: true

Thejus Krishna
  • 1,755
  • 1
  • 19
  • 28
  • I know to get the battery level. But What am looking for is, I wanted to know ONLY the amount of Power consumed by MY APP ALONE. – Manoj Mar 25 '14 at 05:14
  • In automated runs that we have, we add a call before and after the test suite and calculate the difference to understand the consumed battery level. I am not sure if you can get the consumed battery of that particular app. – Thejus Krishna Mar 26 '14 at 05:27
  • Hi @Manoj could please let me know how do you get battery level. I need it – Changdeo Jadhav Jul 07 '14 at 09:06
  • https://developer.apple.com/library/ios/samplecode/BatteryStatus/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008812 – AppleBee Feb 10 '15 at 07:42