tags:

views:

589

answers:

2

I can't find anything definate using my favourite tool however I thought I would put it out here....

Is there a way, using the iPhone SDK, for an App to detect if the device is in a state of receiving power (charging, dock etc.)

I would like to be able to disable the idleTimer automatically if the device is receiving power (otherwise it is a user-specified setting).

Thanks!

+5  A: 

Yes, UIDevice is capable of telling you this:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging) {
    NSLog(@"Device is charging.");
}

See the UIDevice reference in the docs for more info, and for other values of batteryState.

coob
+1  A: 

You are better off using:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

if ([[UIDevice currentDevice] batteryState] != UIDeviceBatteryStateUnplugged) {
      [UIApplication sharedApplication].idleTimerDisabled=YES;
    }

This is because you have to concern yourself with two different states - one is that the battery is charging, and the other when it is fully charged.

If you really wanted to be complete about it - you would register to receive battery monitoring notifications, so you could re-enable the idle timer if the user disconnected main power, etc.

Brad