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.
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.
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);
}