В Viewdidload() я создаю таймер:
Код: Выделить всё
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(handleCountdown), userInfo: nil, repeats: true)
RunLoop.current.add(self.timer!, forMode: RunLoop.Mode.common)
Когда оно входит фон Я аннулирую таймер.
Код: Выделить всё
@objc func applicationDidEnterBackground() {
let defaults = UserDefaults.standard
let quitTime = Date()
defaults.set(quitTime, forKey: "quitTimeKey") //Storing the time of quit in UserDefaults
timer?.invalidate()
}
Код: Выделить всё
@objc func appEntersForeground() {
calculateTimeLeft()
if let timer = timer {
if (!timer.isValid)
{
Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(handleCountdown), userInfo: nil, repeats: true)
}
}
Некоторая помощь будет оценена по достоинству!
Подробнее здесь: https://stackoverflow.com/questions/517 ... foreground