0

整个问题都在标题中。
这就是我所做的。
我测试它我将 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;
    }
}

我错过了什么?

4

1 回答 1

1

我看到的直接问题是,您正在注册以在将来充电状态发生变化时收到通知,但在充电器状态实际发生变化之前没有配置空闲计时器的处理方式。

例如,如果您构建到您的设备,它已插入并充电(可能已满),除非您拔下设备,否则UIDeviceBatteryStateDidChangeNotification没有理由发布。

考虑这样的事情:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(batteryStateDidChange:)
                                             name:UIDeviceBatteryStateDidChangeNotification object:nil];

[self batteryStateDidChange:nil];
于 2013-08-08T11:01:42.270 回答