Maintain state of the app while closed

Hi, I'm working on an app to practice which has a counter with the following:


func start() {
        timerMode = .running
        playSound(sound: "piano-1", type: "mp3")
        timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in
            if self.secondsLeft == 0 {
                self.reset()
                self.next()
                self.start()
            }
            self.secondsLeft -= 1
        })
    }


The problem is that when it reaches 0, it it suppoused to restart the counter, but it doesn't WHEN it's closed or with the screen off, and if I quit the app it resets. How can I keep the state of the app when it's closed or screen off?


Thanks

Answered by KMT in 415675022

Try stuffing that value into userDefaults, then test for it when return from sleep, and poke it in as needed.

Accepted Answer

Try stuffing that value into userDefaults, then test for it when return from sleep, and poke it in as needed.

Maintain state of the app while closed
 
 
Q