I'm putting the finishing touches on my app, but the 5min countdown timer I have running pauses when the app goes to the background. I need an expert to help me solve this, every solution I read I could hardly understand.
I tried watching tutorials with other peoples code but it's hard to implement it to my own not knowing what I'm doing. Since I just borrowed code from someone on GitHub.
countdownTimer.delegate = self
countdownTimer.setTimer(hours: 0, minutes: 0, seconds: selectedSecs)
progressBar.setProgressBar(hours: 0, minutes: 0, seconds: selectedSecs)
stopBtn.isEnabled = false
stopBtn.alpha = 0.5
view.addSubview(messageLabel)
var constraintCenter = NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)
self.view.addConstraint(constraintCenter)
constraintCenter = NSLayoutConstraint(item: messageLabel, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0)
self.view.addConstraint(constraintCenter)
messageLabel.isHidden = true
counterView.isHidden = false
}
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
//MARK: - Countdown Timer Delegate
func countdownTime(time: (hours: String, minutes: String, seconds: String)) {
hours.text = time.hours
minutes.text = time.minutes
seconds.text = time.seconds
}
func countdownTimerDone() {
counterView.isHidden = true
messageLabel.isHidden = false
seconds.text = String(selectedSecs)
countdownTimerDidStart = false
stopBtn.isEnabled = false
stopBtn.alpha = 0.5
startBtn.setTitle("START",for: .normal)
AudioServicesPlaySystemSound(SystemSoundID(1300))
print("countdownTimerDone")
}
//MARK: - Actions
@IBAction func startTimer(_ sender: UIButton) {
messageLabel.isHidden = true
counterView.isHidden = false
stopBtn.isEnabled = true
stopBtn.alpha = 1.0
if !countdownTimerDidStart{
countdownTimer.start()
progressBar.start()
countdownTimerDidStart = true
startBtn.setTitle("PAUSE",for: .normal)
}else{
countdownTimer.pause()
progressBar.pause()
countdownTimerDidStart = false
startBtn.setTitle("RESUME",for: .normal)
}
}
@IBAction func stopTimer(_ sender: UIButton) {
countdownTimer.stop()
progressBar.stop()
countdownTimerDidStart = false
stopBtn.isEnabled = false
stopBtn.alpha = 0.5
startBtn.setTitle("START",for: .normal)
}
I tried using code to stop the timer when it goes to background and update it when it comes to foreground, but I can't find a way to capture the timer value when it closes