Reputation: 10984
I am using :
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batLeft = [myDevice batteryLevel];
int i = [myDevice batteryState];
int batinfo = batLeft * 100;
to find the battery status. I am looking out to find, how to find the time remaining until the charge is complete. ex: 1 hour 20 min remaining. How can I find it programmatically?
Upvotes: 17
Views: 2672
Reputation:
I haven't found any method for this in the official documentation, neither in the class-dumped, private header of the UIDevice
class.
So we have to come up with something. The best "solution" I have in mind at the moment is similar to the approach taken when estimating download time: calculating an average speed of download/charging, and dividing the remaining amount (of data or charge) by that speed:
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
float prevBatteryLev = [UIDevice currentDevice].batteryLevel;
NSDate *startDate = [NSDate date];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(batteryCharged)
name:UIDeviceBatteryLevelDidChangeNotification
object:nil
];
- (void)batteryCharged
{
float currBatteryLev = [UIDevice currentDevice].batteryLevel;
// calculate speed of chargement
float avgChgSpeed = (prevBatteryLev - currBatteryLev) / [startDate timeIntervalSinceNow];
// get how much the battery needs to be charged yet
float remBatteryLev = 1.0 - currBatteryLev;
// divide the two to obtain the remaining charge time
NSTimeInterval remSeconds = remBatteryLev / avgChgSpeed;
// convert/format `remSeconds' as appropriate
}
Upvotes: 10