如何在IOS上使用swift防止我的应用程序上的屏幕锁定

问题描述 投票:88回答:4

如何仅在使用导航时阻止屏幕锁定。 Waze可以选择这样做,我怎样才能在我的应用程序中执行此操作?

ios swift locking screen
4个回答
192
投票

用这个:

Objective-C的:

[[UIApplication sharedApplication] setIdleTimerDisabled: YES];

Swift(遗产):

UIApplication.sharedApplication().idleTimerDisabled = true

迅捷3/4:

UIApplication.shared.isIdleTimerDisabled = true

确保导入UIKit

Here是apple.developer.com文档的链接。


16
投票

对于Swift 3.0,这里有两个选项,具体取决于您要调用代码的位置:

在AppDelegate.swift内:

application.idleTimerDisabled = true

在AppDelegate.swift之外:

UIApplication.shared().isIdleTimerDisabled = true


8
投票

斯威夫特4

在AppDelegate.swift文件中,在应用程序函数中添加以下行:

    application.isIdleTimerDisabled = true

4
投票

你可以使用我的小lib Insomnia(Swift 3,iOS 9+) - 另一个不错的功能是你可以防止只在充电时睡觉。

qazxsw poi解决方案没问题,但你必须记住之后将它设置为qazxsw poi。

© www.soinside.com 2019 - 2024. All rights reserved.