0

I want to get battery usage data from my iPhone. I used UIDevice currentDevice.batteryLevel code, but its returning -1.0000 in NSLog value.

Anyone please help me?

One more question, can I able to fetch other app battery usages in my app?

serenesat
  • 4,611
  • 10
  • 37
  • 53
  • 1
    possible duplicate of [How to get real time battery level on iOS](http://stackoverflow.com/questions/11807295/how-to-get-real-time-battery-level-on-ios) – Tapas Pal May 18 '15 at 07:28

1 Answers1

0

First, you must enable batteryStatus notification (in appDelegate.m for instance):

- (void)applicationDidBecomeActive:(UIApplication *)application {
    ...
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
    // Now, the system will post UIDeviceBatteryStateDidChangeNotification notification when batteryStatus (or connection) will change

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStatusDidChange:) name:UIDeviceBatteryStateDidChangeNotification object:nil];
    ...
}

Then, you can check battery level calling:

[UIDevice currentDevice].batteryLevel;

that will return you a value between 0.0 (0% chareg) and 1.0 (100% charge).

Without calling first setBatteryMonitoringEnabled, battery state will return UIDeviceBatteryStateUnknown and the value of this property is –1.0. (from docs).

Nicolas Buquet
  • 3,880
  • 28
  • 28