Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App is running in background #17

Closed
resand opened this issue Apr 18, 2018 · 6 comments
Closed

App is running in background #17

resand opened this issue Apr 18, 2018 · 6 comments

Comments

@resand
Copy link

resand commented Apr 18, 2018

Continue countdown timer when app is running in background

@app-web-dev
Copy link

Having same issue!

@resand
Copy link
Author

resand commented Apr 25, 2018

@app-web-dev I solved it with observers!

@resand resand closed this as completed May 23, 2018
@ajeetpratap
Copy link

I am stuck with this same issue.. I want the timer to run even the app goes in background. @resand How you solved it with observer?

@resand
Copy link
Author

resand commented Jun 27, 2019

@ajeetpratap Use an observer when sending the app to the background and disable the timer, returning to the foreground subtractthe time difference and initialize the timer again.

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: .UIApplicationWillResignActive, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: .UIApplicationDidBecomeActive, object: nil)
}

@objc func willResignActive(_ notification: Notification) {
     currentBackgroundDate = Date()
     timer.invalidate()
}
    
@objc func didBecomeActive(_ notification: Notification) {
     let calender: Calendar = Calendar.current
     let components: DateComponents = calender.dateComponents([.year, .month, .day, .hour, .minute, .second], from: currentBackgroundDate!, to: Date())
     let seconds = components.second
     let secondsUpdate = countDownView.currentSecond - seconds!
        
     countDownView.start(beginingValue: secondsUpdate, interval: 1)
}

@ajeetpratap
Copy link

@resand Thanks for the quick reply:
countDownView.start(beginingValue: secondsUpdate, interval: 1) will start the timer again and it will also update the UI also.I want to persist the UI and resume it again when I come to foreground.
Any thoughts on that?

@resand
Copy link
Author

resand commented Jun 27, 2019

@ajeetpratap You could check if the library has a function to get the time difference and keep the UI.
He served me as an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants