iOS App prevents device from sleep mode
Asked Answered
B

3

19

I've a really strange behavior of my iOS app. It prevents the device from going to standby. I've already searched if there is anywhere the idleTimeDisabled flag set, but this isn't the case. The idle timer inside the settings app is set to 2 minutes and works within other apps. The device isn't jailbroken and the even restarting the device didn't help. The app was written by somebody else and I'm new to it. So in the moment I don't have any idea what to look for. Has anybody of you an idea, why this could be the case or how I could figure out what's the reason for this behavior?

Thank you very much!

Blacking answered 4/3, 2014 at 6:52 Comment(7)
Did you find any instance of [[UIApplication sharedApplication] setIdleTimerDisabled:No]; in your code?Cumber
#493995Homoiousian
Is this while running the app in Xcode?Cano
Thank you all for your fast answers. The [[UIApplication sharedApplication] setIdleTimerDisabled:NO]; is set nowhere and the app wasn't started by Xcode.Blacking
@Blacking what was the problem. do you resolved it?Remise
@MohammadSadiqShaikh it is not resolved. Issue is related to AVPlayer. There is no answer for this case.Mystic
Can you provide a code samples? appDelegate and relevant VCsAudubon
G
15

If your app is using AVPlayer or some other similar AVFoundation functionality, this can be the cause.

If AVPlayer is playing a video, a device will not go to sleep while a video is playing.

If the video is playing in a loop, the device will never go to sleep.

Edit: With iOS 12 this is now possible by setting

player.preventsDisplaySleepDuringVideoPlayback = false
Giacobo answered 10/6, 2015 at 11:44 Comment(3)
Looks like you are right. Maybe someone knows workaround to fix this? We use AVPlayer for interactive background.Mystic
I don't know if there's a way to tell AVPlayer to not block sleep mode. You can try set idleTimerDisable:NO after AVPlayer starts playing, or implement an idle timer by yourself and pause the playback :/Affixation
Multiple[1] people[2] have reported that you cannot get around the screen lock while an AVPlayer is playing. [1] https://mcmap.net/q/666243/-allow-quot-auto-lock-quot-while-video-is-being-played [2] https://mcmap.net/q/666244/-how-to-enable-iphone-auto-lock-during-mpmovieplayer-playbackBeverly
K
13

Put this line in your ViewController that require not being in sleep mode

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

Important: You should set this property only if necessary and should be sure to reset it to NO when the need no longer exists. Most applications should let the system turn off the screen when the idle timer elapses. This includes audio applications. With appropriate use of Audio Session Services, playback and recording proceed uninterrupted when the screen turns off. The only applications that should disable the idle timer are mapping applications, games, or similar programs with sporadic user interaction.

Klimesh answered 4/3, 2014 at 8:41 Comment(1)
Thank you for your answer Mutawe. But the problem isn't that the app or one of it's Viewcontroller should be kept always on. The app does this (unfortunately) by itself and prevents the device from standby. The setIdleTimerDisabled flag isn't existing inside the app.Blacking
J
8

Make sure you are not "testing" with your Xcode. Because it will always remained as turn on status as long as you plugin your iPhone with a cable.

Jardine answered 4/8, 2015 at 4:1 Comment(1)
I too have the feeling this is something Xcode does. Rebooting the device helps.Godrich

© 2022 - 2024 — McMap. All rights reserved.