[英]How to prevent screen lock on my application with swift on iOS
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 可以选择这样做,我如何在我的应用程序中做到这一点?
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
。
Here is the link to the documentation from apple.developer.com. 这是来自 apple.developer.com 的文档链接。
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
Outside AppDelegate.swift:在 AppDelegate.swift 之外:
UIApplication.shared().isIdleTimerDisabled = true
Swift 4斯威夫特 4
in AppDelegate.swift file, add the following line inside application function:在 AppDelegate.swift 文件中,在应用程序函数中添加以下行:
application.isIdleTimerDisabled = true
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 idleTimerDisabled
soultion is okay but you have to remember to set it to false
afterwards. idleTimerDisabled
是好的,但您必须记住之后将其设置为false
。
If you have more advanced case you can use our small project: ScreenSleepManager or if it's just about particular ViewControllers - use Insomnia as pointed earlier.如果你有更高级的案例,你可以使用我们的小项目: ScreenSleepManager或者如果它只是关于特定的ViewControllers - 使用前面指出的Insomnia 。 Manual dealing with
idleTimerDisabled
almost always caused me some issues (like forgot to re-set to false or handle multiple (nested) modules trying to set it).手动处理
idleTimerDisabled
几乎总是给我带来一些问题(比如忘记重新设置为 false 或处理多个(嵌套)模块试图设置它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.