【问题标题】:How to prevent iPhone to lock when my app is charging如何在我的应用程序充电时防止 iPhone 锁定
【发布时间】:2013-08-08 10:55:39
【问题描述】:

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

我错过了什么?

【问题讨论】:

    标签: iphone ios objective-c uidevice idle-timer


    【解决方案1】:

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

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

    考虑这样的事情:

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

    【讨论】:

      猜你喜欢
      • 2012-01-28
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多