-2

Possible Duplicate:
How to get battery status?

I am implementing one iPhone application in which i want to read battery status of the iPhone by programming. I dont know is it possible or not. Please give me advice for that.

Community
  • 1
  • 1
ManthanDalal
  • 1,063
  • 2
  • 20
  • 45

2 Answers2

4

To read battery level and status you just have to:

float battLvl = [[UIDevice currentDevice] batteryLevel]; // range is from 0.0 to 1.0
UIDeviceBatteryState battState = [UIDevice currentDevice] batteryStatus];

To monitor battery level and status you can do the following:

// enable monitoring and add observers with selector
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device];

// Your notification handler
- (void)batteryChanged:(NSNotification *)notification
{
    UIDevice *device = [UIDevice currentDevice];
    NSLog(@"state: %i | charge: %f", device.batteryState, device.batteryLevel);
}
matm
  • 7,059
  • 5
  • 36
  • 50
0

Following lines of code will be helpful:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

[[UIDevice currentDevice] batteryLevel];
[[UIDevice currentDevice] batteryState];

For detail, refer THIS link. It will elaborate you.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109