Detect if device is charging

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.

Leave a Comment