How do I get the battery status on an iPhone?
5 Answers
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batLeft = [myDevice batteryLevel];
int i=[myDevice batteryState];
int batinfo=(batLeft*100);
NSLog(@"Battry Level is :%d and Battery Status is :%d",batinfo,i);
switch (i)
{
case UIDeviceBatteryStateUnplugged:
{
[BCStatus setText:NSLocalizedString(@"UnpluggedKey", @"")];
break;
}
case UIDeviceBatteryStateCharging:
{
[BCStatus setText:NSLocalizedString(@"ChargingKey", @"")];
break;
}
case UIDeviceBatteryStateFull:
{
[BCStatus setText:NSLocalizedString(@"FullKey", @"")];
break;
}
default:
{
[BCStatus setText:NSLocalizedString(@"UnknownKey", @"")];
break;
}
}
BCStatus is uilabel.

- 25,802
- 10
- 92
- 123

- 1,795
- 2
- 27
- 47
-
4worth noting that the associated notifications `UIDeviceBatteryLevelDidChangeNotification` and `UIDeviceBatteryStateDidChangeNotification` do not get sent if your app is executing in background – yano Jul 26 '12 at 23:46
-
what is battery status here? – Code cracker Aug 14 '15 at 08:09
-
@Codecracker batteryLevel is float value ranging from 0.0 to 1.0 and batteryState is some kind of code which states device is charged, in charging, etc. – Paresh Thakor Aug 14 '15 at 08:44
-
worth noting that 'setBatteryMonitoringEnabled' method isn't thread safe, as mentioned in https://github.com/jszumski/uidevice-threading-crash, http://openradar.appspot.com/13941890, https://twitter.com/cheesemaker/status/517373206748205056. – yonivav Jan 03 '17 at 10:02
Iphone SDK 3.0 beta supports this.

- 2,410
- 6
- 34
- 56
-
...and then again, it might have been announced by Apple in a public forum. – Rog Apr 01 '09 at 15:38
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

- 4,052
- 2
- 36
- 37
Here's what I used for my string as a quick utility method, note that you have to enable battery monitoring to get a value, and then if you don't want to get the notifications (obviously some efficiency to be gained there, since they give you the ability to turn it off) then you should turn it off again after (like I do in this function):
NSString *statusString(void)
{
UIDevice *device = [UIDevice currentDevice];
NSString *batteryStateString = nil;
switch(device.batteryState)
{
case UIDeviceBatteryStateUnplugged: batteryStateString = @"Unplugged"; break;
case UIDeviceBatteryStateCharging: batteryStateString = @"Charging"; break;
case UIDeviceBatteryStateFull: batteryStateString = @"Full"; break;
default: batteryStateString = @"Unknown"; break;
}
[device setBatteryMonitoringEnabled:YES];
NSString *statusString = [NSString stringWithFormat:@"Battery Level - %d%%, Battery State - %@",
(int)round(device.batteryLevel * 100), batteryStateString];
[device setBatteryMonitoringEnabled:NO];
return statusString;
}

- 25,802
- 10
- 92
- 123
Now that the 3.1 SDK is released look for the Getting the Device Battery State section in UIDevice's documentation. It is abunch of battery* properties.

- 7,349
- 1
- 36
- 78