如何在应用程序处于前台模式时禁用 iPhone/iPad 自动锁定?

发布于 2025-01-08 09:15:43 字数 243 浏览 1 评论 0原文

我正在开发一个音乐/视频播放器应用程序,只需要知道如何在我的应用程序位于前台时禁用自动锁定。

我知道我必须在某些时候使用 [[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小傻瓜 2025-01-15 09:15:43

启用空闲计时器

- (void)applicationWillResignActive:(UIApplication *)application

并禁用它

- (void)applicationDidBecomeActive:(UIApplication *)application

Enable the idle timer in

- (void)applicationWillResignActive:(UIApplication *)application

and disable it in

- (void)applicationDidBecomeActive:(UIApplication *)application
苍景流年 2025-01-15 09:15:43

禁用它的最佳位置是在 didFinishLaunchingWithOptions 中。当应用程序在后台运行时,系统会自动使设置无效。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    application.idleTimerDisabled = YES; 
    return YES;
}

我发布了这个替代方案,因为接受的答案不会阻止出现警报(电子邮件、消息、日历事件等)或通知中心或控制中心启动时的自动锁定。

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.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    application.idleTimerDisabled = YES; 
    return YES;
}

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.

冰雪之触 2025-01-15 09:15:43

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

绅刃 2025-01-15 09:15:43

在 Swift 3.0 中:

UIApplication.shared().isIdleTimerDisabled = true

And in Swift 3.0:

UIApplication.shared().isIdleTimerDisabled = true
悲喜皆因你 2025-01-15 09:15:43

我的 2 美分:对于 xcode 9:

 application.idleTimerDisabled = true

.....AppDelegate.swift:28:15:“idleTimerDisabled”已重命名为“isIdleTimerDisabled”,

因此:

application.isIdleTimerDisabled = true

my 2 cents: for xcode 9:

 application.idleTimerDisabled = true

.....AppDelegate.swift:28:15: 'idleTimerDisabled' has been renamed to 'isIdleTimerDisabled'

so:

application.isIdleTimerDisabled = true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文