Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
What I have done
I have tested my app with the following code on a physical iPhone 6 running on iOS 12.1.12. I set the phone
Dislpay & Brigthness > Auto-Lock
to 30 seconds. I ran the app and on the particular VC with
UIApplication.shared.isIdleTimerDisabled = true
, the screen remains on even after 30 seconds. And when I switch to other VC without
UIApplication.shared.isIdleTimerDisabled = true
, the screen will turn off after 30 seconds.
I distributed to AppStore and I am positive that I have uploaded the correct version and dowloaded a fresh copy from AppStore, the screen always on thingy doesn't work at all, the screen will always turn off following the
Auto-Lock
setting.
I have read some articles
https://docs.kioskproapp.com/article/899-ios-12-with-guided-access-causing-kiosk-pro-to-sleep
and
isIdleTimerDisabled not working in iOS 12
I tried the
Guided Access
and switched on
Guided Access
and
Mirror Display Auto-Lock > ON
. Still it is not working for the App from AppStore. Please help.
override func viewDidLoad() {
super.viewDidLoad()
//==== For the screen to remains on
UIApplication.shared.isIdleTimerDisabled = true
override func viewDidDisappear(_ animated: Bool) {
//=== Switch off the screen always on, back to the phone settings.
UIApplication.shared.isIdleTimerDisabled = false
–
–
–
I had the same issue on iOS 13. And I found this great article explaining why .isIdleTimerDiabled
appears to be not working.
In short, as iOS 13 introduced SceneDelegate, just by setting .isIdleTimerDisabled
to false in AppDelegate is not enough, you need to set it in SceneDelegate too
class SceneDelegate: NSObject, UIWindowSceneDelegate {
var window: UIWindow?
@available(iOS 13.0, *)
func sceneDidBecomeActive(_ scene: UIScene) {
UIApplication.shared.isIdleTimerDisabled = true
Code above made my app immune to autolock, but this is just a quick dirty solution, a better way is to toggle isIdleTimerDisabled
on and off based on ViewController user is using, below is Apple's documentation about this property.
You should set this property (isIdleTimerDisabled
) only if necessary and should be sure to reset it to false when the need no longer exists. Most apps should let the system turn off the screen when the idle timer elapses. This includes audio apps. With appropriate use of Audio Session Services, playback and recording proceed uninterrupted when the screen turns off. The only apps that should disable the idle timer are mapping apps, games, or programs where the app needs to continue displaying content when user interaction is minimal.
–
Just wanted to also share that the provided answer from https://stackoverflow.com/a/64235679/14414215 @Daniel Hu was great but I had issues why it wasn't working while running it thru Xcode. This little snippet was the key (from This Link)
Test Note: Running applications under the debugger through Xcode will
automatically disable the idle timer. When building or testing the
app, make sure to install it, then launch it “by hand” on a test
device. The effects will be most obvious if the “Auto-Lock” setting is
30 seconds.
Here's how to set isIdleTimerDisabled
from the scene delegate for a SwiftUI application on iOS 13+:
import SwiftUI
@main
struct MyApp: App {
@Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
MainView()
.onChange(of: scenePhase) { (phase) in
switch phase {
case .active:
// In iOS 13+, idle timer needs to be set in scene to override default
UIApplication.shared.isIdleTimerDisabled = true
case .inactive: break
case .background: break
@unknown default: print("ScenePhase: unexpected state")
You must be opening another view controller from current view controller. That set the value of isIdleTimerDisabled to false when viewDidDisappear calls.
So, you should set UIApplication.shared.isIdleTimerDisabled = true in viewDidAppear. Hope this will solve your issue.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.