0

I have an app with a sleep timer where the user can select that after a certain duration of time, the app will suspend. Basically what I do is:

  1. Use this code at the start of the app to disable the app timer:

    [[UIApplication sharedApplication] setIdleTimerDisabled: YES];

  2. When the sleep timer expires:

    [[UIApplication sharedApplication] setIdleTimerDisabled: NO];

Once the the Auto-Lock value in the user preferences is up, the screen dims and the app goes asleep.

It was working fine but no longer for some reason. I'm troubleshooting and have commented out the commands involving setIdleTimerDisabled still it never goes to sleep.

If I test the state of the idleTimer with ([UIApplication sharedApplication].isIdleTimerDisabled) I can see that IdleTimer is not actually disabled so it can't be that.

What else could be preventing a device from sleeping? when app is left without any interaction. My device is running 9.3.3 in case that's useful.

pingin
  • 482
  • 1
  • 6
  • 19

1 Answers1

0

I think I found the answer — there were actually two problems. 1. So long as I'm running and building on Xcode, the app will never go to sleep (I think). 2. My app has sound so I'm doing something like this to ensure the controls and audio still work in when the device is locked:

audioSession = [AVAudioSession sharedInstance];
    NSError *setCategoryError = nil;
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];

This however prevents the device from sleeping — the sound keeps playing. So what I do now is, once the screen is locked, I change the category of the audioSession to

[audioSession setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError];

That however causes another problem which I haven't figured out yet…

pingin
  • 482
  • 1
  • 6
  • 19