整个问题都在标题中。
这就是我所做的。
我测试它我将 iPhone 插入我的 Mac 不充电我不知道这是否重要。
- (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;
}
}
我错过了什么?