6

My iPhone application has two states: UI and Game. Game is played using device tilting only so I switched auto-sleep off on game start:

[[UIApplication sharedApplication].idleTimerDisabled = TRUE;

But as soon as I return to UI, I want auto-sleep to be active again. So on game finish I restore it:

[[UIApplication sharedApplication].idleTimerDisabled = FALSE;

After a long game playing, it resulted in immediate darkening of the first UI screen that I go after the game. So it seems that when idle timer was disabled it was still calculating time. And it had fired immediately after "enabling". How can I fix this problem?

THelper
  • 15,333
  • 6
  • 64
  • 104
Nick
  • 3,205
  • 9
  • 57
  • 108

2 Answers2

2

I don't know if the idleTimer can be reset programmatically, but an option is to require the user to touch the screen before it goes back.

Another option is set your own timeout once you are back and wait for it to complete before you set idleTimerDisabled = NO. Remember to clear this timeout if you start the game again.

Gil
  • 3,529
  • 1
  • 19
  • 22
  • 1
    The second option is exactly what I use already. I wonder if there exist less "hacky" way to amend this problem. – Nick Nov 20 '11 at 12:20
  • 1
    Guessing the system idle timeout value is not so nice, but other than that it is a pretty nice "hack". You could try a programmatic touch event, but even if it works it won't be less "hacky". – Gil Nov 20 '11 at 14:54
  • @Gil Maybe im not understanding this right. How would arbitrarily delaying setting the idleTimerDisabled=NO call help with the "race condition" that the OP is seeing? For example, what if the iOS timer was set to 30 seconds and was currently at sec 25, then the app forces a 5 sec delay once it's back. Wouldnt that just cause the same issue since you don't know the status of the iOS internal idle timer? – stonedauwg Jun 28 '16 at 16:24
  • 1
    @stonedauwg There is no race condition. My suggestion is to delay setting `idleTimerDisabled` for long enough, that the user has a chance to interact with the UI. I would probably delay it 30 seconds, which is the lowest configurable auto lock delay. – Gil Jun 28 '16 at 16:53
  • @Gil I see. Yes, I think I like that approach after all. Thanks – stonedauwg Jun 28 '16 at 21:30
1

This answer may be usefull.

I also had problems when using Music/Audio players, which seemed to reactivate the timer.

ps: in ObjectiveC you should use YES/NO instead of FALSE/TRUE

Community
  • 1
  • 1
Daniel
  • 20,420
  • 10
  • 92
  • 149
  • 1
    I do not see direct relation between my question and the answer you mentioned. As I understood, they had problems with activating disable_timer. And their solution was to set it to NO and then immediately to YES. My situation is that timer activates and deactivates normally, but it "remembers" length of its deactivation time and, on activation, fires at once. Isn't there a way to programmatically "touch" device thus resetting timer? – Nick Nov 18 '11 at 13:40
  • I think this tip solved my issue with the idle timer which I was setting to the value of a UISwitch (e.g. 0/1) – GeoffCoope Feb 22 '13 at 01:38