如何防止iPhone在我的应用程序充电时锁定



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

我错过了什么?

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

例如,如果你根据自己的设备构建,它已经插上电源并充电(可能已满),除非你拔下设备,否则UIDeviceBatteryStateDidChangeNotification没有理由发布。

考虑一下这样的东西:

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

相关内容

  • 没有找到相关文章

最新更新