简体   繁体   English

如何在“闲置计时器”上使用计时器

[英]How to use a timer on “idle timer”

I am making an app where I would like to make the app go to sleep in 2 hours after a button is pressed. 我正在制作一个应用程序,我想让该应用程序在按下按钮后2小时内进入睡眠状态。 I have seen some stuff online with using "idle timer" to deactivate it. 我在网上看到了一些使用“空闲计时器”将其停用的内容。 But I have not seen how you put this on a timer. 但是我还没有看到你如何把它放在计时器上。

Well, turn off device programmatically is not possible. 嗯,无法以编程方式关闭设备。 It is possible to put app to sleep if idle timer was used to make it always on. 如果使用空闲计时器使应用程序始终打开,则可以使应用程序进入睡眠状态。

So, somewhere at start of app: 因此,在应用程序启动的某个位置:

UIApplication.shared.isIdleTimerDisabled = true;

Now when app is running display will be always on. 现在,当应用程序运行时,显示屏将始终处于打开状态。

Now user hit "Sleep in 2hours" button. 现在,用户点击“ 2小时内睡眠”按钮。 Inside button handler code insert this: 在按钮处理程序代码内部插入以下代码:

_ = Timer.scheduledTimer(withTimeInterval: 3600*2, repeats: false, block: { (Timer) in
        UIApplication.shared.isIdleTimerDisabled = false;
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM