Reputation: 17
I have a problem trying to update the label when the app returns from Background. I already implemented LocalNotifications for my timer app and is working perfect but when I return to the app the countdown label is stopped. So basically I am trying to find the way to update the countdown label with the current time left. I would appreciate any help!
My Code is here:
var timer = Timer()
var teas = Teas()
var isTimerRunning = false
var alarmSound = AVAudioPlayer()
var playing = false
var initialValueOfTime = 0
var timeDuration = 0
var currentBackgroundDate = Date()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(teasGoBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(runTimer), name: UIApplication.didBecomeActiveNotification, object: nil)
}
@objc func runTimer() {
if isTimerRunning {
teas.time -= 1
let difference = Date().timeIntervalSince(self.currentBackgroundDate)
timeDuration = timeDuration + Int(difference)
if teas.time <= 0 {
timer.invalidate()
startButton.setTitle("Done", for: .normal)
}
}
else {
teas.time = initialValueOfTime
}
@objc func resetTimer() {
timer.invalidate()
runTimer()
}
@objc func teasGoBackground() {
timer.invalidate()
currentBackgroundDate = Date()
}
@IBAction func startButtonPressed(_ sender: Any) {
if !isTimerRunning {
navigationItem.hidesBackButton = true
isTimerRunning = true
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(runTimer), userInfo: nil, repeats: true)
print("time")
startButton.setTitle("Stop Brewing", for: .normal)
} else {
navigationItem.hidesBackButton = false
isTimerRunning = false
resetTimer()
startButton.setTitle("Start Brewing", for: .normal)
}
}
Upvotes: 0
Views: 349
Reputation: 17
Finally I implemented this code to my project and worked!
if let date = UserDefaults.standard.object(forKey: "teastart\(teas.id)") as? Date {
let now = Date()
let secondsTillNow = now.timeIntervalSince1970
let previousSeconds = date.timeIntervalSince1970
let timeSinceStart = secondsTillNow - previousSeconds
let time = Int(timeSinceStart)
if time > teas.time {
initialValueOfTime = teas.time
startButton.setTitle(NSLocalizedString("Start Brewing", comment: ""), for: .normal)
UserDefaults.standard.set(false, forKey: "timerunning\(teas.id)")
} else {
initialValueOfTime = teas.time - time
startButton.setTitle(NSLocalizedString("Stop Brewing", comment: ""), for: .normal)
UserDefaults.standard.set(true, forKey: "timerunning\(teas.id)")
}
} else {
initialValueOfTime = teas.time
UserDefaults.standard.set(false, forKey: "timerunning\(teas.id)")
}
Upvotes: 0
Reputation: 1125
You need to add your observers inside viewDidLoad()
, NOT in viewDidAppear
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(teasGoBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(runTimer), name: UIApplication.didBecomeActiveNotification, object: nil)
}
Upvotes: 1