如何在应用程序处于前台模式时禁用 iPhone/iPad 自动锁定?
我正在开发一个音乐/视频播放器应用程序,只需要知道如何在我的应用程序位于前台时禁用自动锁定。
我知道我必须在某些时候使用 [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
和 [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
,但是在哪里放置它们的最佳位置?
I'm developing an music/video player app and just need to konw how to disable the auto-lock while my app is in foreground.
I know I've to use [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
and [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
at some point, but where is the best place to put them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
启用空闲计时器
并禁用它
Enable the idle timer in
and disable it in
禁用它的最佳位置是在 didFinishLaunchingWithOptions 中。当应用程序在后台运行时,系统会自动使设置无效。
我发布了这个替代方案,因为接受的答案不会阻止出现警报(电子邮件、消息、日历事件等)或通知中心或控制中心启动时的自动锁定。
The best place to disable it is in didFinishLaunchingWithOptions. The system will automatically take care of making the setting have no effect when the app is in the background.
I posted this alternative because the accepted answer does not prevent auto-lock when an alert appears (email, message, calendar event etc), or the notification center or control center is up.
Swift 3.0:
AppDelegate.swift 内部:
application.idleTimerDisabled = true
外部AppDelegate.swift:
UIApplication.shared().isIdleTimerDisabled = true
Swift 3.0:
Inside AppDelegate.swift:
application.idleTimerDisabled = true
Outside AppDelegate.swift:
UIApplication.shared().isIdleTimerDisabled = true
在 Swift 3.0 中:
And in Swift 3.0:
我的 2 美分:对于 xcode 9:
.....AppDelegate.swift:28:15:“idleTimerDisabled”已重命名为“isIdleTimerDisabled”,
因此:
my 2 cents: for xcode 9:
.....AppDelegate.swift:28:15: 'idleTimerDisabled' has been renamed to 'isIdleTimerDisabled'
so: