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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28329185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:41:09  来源:igfitidea点击:

How to prevent screen lock on my application with swift on iOS

iosswiftlockingscreen

提问by alvarogalia

How can I prevent screen lock only when using Navigation?

如何仅在使用导航时防止屏幕锁定?

Waze has the option to do that, how can I do this in my App?

Waze 可以选择这样做,我怎样才能在我的应用程序中做到这一点?

回答by atwalsh

Use this:

用这个:

Objective-C:

目标-C:

[[UIApplication sharedApplication] setIdleTimerDisabled: YES];

Swift (legacy):

斯威夫特(遗留):

UIApplication.sharedApplication().idleTimerDisabled = true

Swift 3 and above:

Swift 3 及以上:

UIApplication.shared.isIdleTimerDisabled = true

Make sure to import UIKit.

确保导入UIKit.

Hereis the link to the documentation from apple.developer.com.

是来自 apple.developer.com 的文档链接。

回答by Crashalot

For Swift 3.0 here are two options depending on where you want to invoke the code:

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

Inside AppDelegate.swift:

在 AppDelegate.swift 中:

application.idleTimerDisabled = true

application.idleTimerDisabled = true

Outside AppDelegate.swift:

在 AppDelegate.swift 之外:

UIApplication.shared().isIdleTimerDisabled = true

UIApplication.shared().isIdleTimerDisabled = true

回答by kavehmb

Swift 4

斯威夫特 4

in AppDelegate.swift file, add the following line inside applicationfunction:

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

    application.isIdleTimerDisabled = true

回答by nsmeme

You can use my little lib Insomnia(Swift 3, iOS 9+) - another nice feature is that you can prevent from sleeping only when charging.

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

The idleTimerDisabledsoultion is okay but you have to remember to set it to falseafterwards.

idleTimerDisabledsoultion是好的,但你要记住将其设置为false之后。

回答by Dev_IL

If you have more advanced case you can use our small project: ScreenSleepManageror if it's just about particular ViewControllers- use Insomniaas pointed earlier. Manual dealing with idleTimerDisabledalmost always caused me some issues (like forgot to re-set to false or handle multiple (nested) modules trying to set it).

如果你有更高级的案例,你可以使用我们的小项目:ScreenSleepManager或者如果它只是关于特定的ViewControllers- 使用前面指出的Insomnia。手动处理idleTimerDisabled几乎总是给我带来一些问题(比如忘记重新设置为 false 或处理多个(嵌套)模块试图设置它)。