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!

Alex
  • 283
  • 1
  • 2
  • 5

3 Answers3

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
hashier
  • 4,670
  • 1
  • 28
  • 41
AndroC
  • 4,758
  • 2
  • 46
  • 69
  • Looks like you are right. Maybe someone knows workaround to fix this? We use AVPlayer for interactive background. – Timur Bernikovich Jul 12 '17 at 15:34
  • 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 :/ – Bruno Pinheiro Jul 17 '17 at 15:28
  • 1
    Multiple[1] people[2] have reported that you cannot get around the screen lock while an AVPlayer is playing. [1] https://stackoverflow.com/a/38018555/424210 [2] https://stackoverflow.com/a/2489351/424210 – Clay Jul 19 '17 at 13:42
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.

Mutawe
  • 6,464
  • 3
  • 47
  • 90
  • 4
    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. – Alex Mar 04 '14 at 09:21
6

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.

Ted Kim
  • 61
  • 1
  • 2
  • 1
    I too have the feeling this is something Xcode does. Rebooting the device helps. – bio Feb 17 '18 at 10:42