我正在编写一个 iPhone 应用程序。当应用程序运行且 iPhone 正在充电时,无需锁定 iPhone。当设备正在充电且我的应用程序正在运行时,是否可以防止 iPhone 被锁定?
最佳答案
您可以订阅 UIDeviceBatteryStateDidChangeNotification
通知来获取 iPhone 开始/停止充电的时刻。然后,如果 iPhone 正在充电,您可以将 UIApplication 对象中的idleTimerDisabled 属性设置为 YES,以防止设备进入休眠状态:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateBatteryState:)
name:UIDeviceBatteryStateDidChangeNotification
object:nil];
- (void) updateBatteryState:(NSNotification*)notification{
[UIApplication sharedApplication].idleTimerDisabled =
([UIDevice currentDevice].batteryState == UIDeviceBatteryStateCharging);
}
附注如果用户决定使用 sleep /唤醒按钮让设备进入休眠状态,则无法阻止他这样做
关于iphone - 防止 iPhone 在连接到充电器且应用程序正在运行时锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8169735/