Whole question is in title.
Here what I did.
I test it I plug iPhone to my mac not to charger I don;t know if this is important.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{...
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(batteryStateDidChange:)
name:UIDeviceBatteryStateDidChangeNotification object:nil];
...}
- (void)batteryStateDidChange:(NSNotification *)notification
{
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging ||
[[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateFull ){
[UIApplication sharedApplication].idleTimerDisabled = YES;
}
else if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateUnplugged) {
[UIApplication sharedApplication].idleTimerDisabled = NO;
}
}
What am I missing?