1

I created a 4:00 minute countdown timer with an acoustic signal when it reaches 0:00. I used a function that prevents the phone from falling asleep while the timer is running. However, this is the last screen of my app. So once the acoustig final signal is over, I'd like the app to slowly shut down. I don't know where to place the code.

This is the code I used to prevent it from falling asleep:

override func viewDidLoad() {
    super.viewDidLoad()

    UIApplication.sharedApplication().idleTimerDisabled = true
}

So I thought I could use

UIApplication.sharedApplication().idleTimerDisabled = false

to reverse it. When I place it inside the stopTimer function, it shuts down immediately at 0:00.

func stopTimer() {
    if count == 0 {
        timer.invalidate()
        timerRunning = false
        playSound()
        timerLabel.text = "DONE"
    }
}

Maybe there's a way to lag the process so that it shuts down after 15 seconds. How do I do this?

luk2302
  • 46,204
  • 19
  • 86
  • 119
mojomo
  • 109
  • 11
  • 1
    You could simply place it in a `dispatch_after`-block: http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift – luk2302 Jan 23 '16 at 16:33
  • I am still unsure wether or not you *want* to instantly go to sleep after the timer runs out - or if you want to *"smoothly"* go to sleep waiting 15 secs or so after the timer runs out. Which one do you want? – luk2302 Jan 23 '16 at 16:55
  • @luk2302: Smoothly, after 15-30 seconds, when there's no user activity after the timer runs out. – mojomo Jan 23 '16 at 16:57

2 Answers2

2

As written in my comment and in the linked answer you could use dispatch_after:

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(15 * Double(NSEC_PER_SEC))) // the 15 indicates the seconds to wait
dispatch_after(delayTime, dispatch_get_main_queue()) {
    UIApplication.sharedApplication().idleTimerDisabled = false
}
Community
  • 1
  • 1
luk2302
  • 46,204
  • 19
  • 86
  • 119
0

Apps should not terminate themselves. The app store will reject you if you do that. Simply set idleTimerDisabled to false once your timer finishes. The system will then go to sleep at some point in the future based on the user's settings and current activity. (The inactivity sleep timer doesn't start as long as the user is interacting with their device.)

Duncan C
  • 115,063
  • 19
  • 151
  • 241
  • 2
    That is exactly what OP does already. He is not really asking about "terminating", but for how to enable the idleTimer again after he is finished. (at least that is how I understand the question) – luk2302 Jan 23 '16 at 16:36
  • To clarify: The timer runs for 4:00 minutes. Usually my phone would shut down after 2 minutes. This is why I used the code to prevent it from going asleep. But, once the countdown is over, it can go to sleep. Not immediately, but after a couple of seconds. So setting the same code to 'false' to restore the default behavior of the phone seemed logical. @luk2302 The dispatch solution looks interesting, but I'm new to Swift, and I don't really understand it. Where do I place the seconds? – mojomo Jan 23 '16 at 16:53