Continue countdown timer when app is running in background/suspended

Related searches

I have a functional countdown timer.

The problem is that I need to continue the countdown when the user puts the app in the background. Or suspends the app? I am not sure what the correct terminology is to describe this action.

In other words, the user opens the app and starts the countdown. The user should be able to use other apps but the countdown still operate. Also, the countdown should be able to run forever until it finishes or when the user terminates running the app.

Here is the code for my timer:

    class Timer{
var timer = NSTimer();
// the callback to be invoked everytime the timer 'ticks'
var handler: (Int) -> ();
//the total duration in seconds for which the timer should run to be set by the caller
let duration: Int;
//the amount of time in seconds elapsed so far
var elapsedTime: Int = 0;
var targetController = WaitingRoomController.self

var completionCallback: (() -> Void)!

:param: an integer duration specifying the total time in seconds for which the timer should run repeatedly
:param: handler is reference to a function that takes an Integer argument representing the elapsed time allowing the implementor to process elapsed time and returns void
init(duration: Int , handler : (Int) -> ()){
    self.duration = duration;
    self.handler = handler;

Schedule the Timer to run every 1 second and invoke a callback method specified by 'selector' in repeating mode
func start(){
    self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "onTick", userInfo: nil, repeats: true);

invalidate the timer
func stop(){
    println("timer was invaidated from stop()")

Called everytime the timer 'ticks'. Keep track of the total time elapsed and trigger the handler to notify the implementors of the current 'tick'. If the amount of time elapsed is the same as the total duration for the timer was intended to run, stop the timer.

@objc func onTick() {
    //increment the elapsed time by 1 second
    //Notify the implementors of the updated value of elapsed time
    //If the amount of elapsed time in seconds is same as the total time in seconds for which this timer was intended to run, stop the timer
    if self.elapsedTime == self.duration {

    println("timer was invalidated from deinit()")


What I suggest is cancel the timer and store a NSDate when the app goes to the background. You can use this notification to detect the app going to the background:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseApp", name: UIApplicationDidEnterBackgroundNotification, object: nil)

Then cancel the timer and store the date:

func pauseApp(){
    self.stop() //invalidate timer
    self.currentBackgroundDate = NSDate()

Use this notification to detect the user coming back:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "startApp", name: UIApplicationDidBecomeActiveNotification, object: nil)

Then calculate the difference from the stored date to the current date, update your counter and start the timer again:

func startApp(){
    let difference = self.currentBackgroundDate.timeIntervalSinceDate(NSDate())
    self.handler(difference) //update difference
    self.start() //start timer

How to run timer in background for…, Either your app is suspended (which will prevent the timer from running) or your app is terminated. This state In case A, the system will resume your app in the background. countdownTimer, forMode: .common) }. Timer Control Running In Background Submitted by SergioGiusti on ‎11-02-2018 08:07 AM Can we get the timer control allowed to continue running on a mobile device even when another application is opened instead of powerapps.

You shouldn't do that. A running timer keeps the device's processor running at full power, which is bad.

Only certain types of apps are allowed to actually run code indefinitely from the background, e.g. VOIP apps and music apps.

A couple of options:

  1. Set up a local notification for a future date (which will send a message to your app, or re-launch it if it was no longer running.)

  2. When you start your timer, record the current NSDate. Then, when your app returns to the foreground or gets re-launched, compare the current date to the saved date, figure out the amount of time that's elapsed, and decide if your timer is finished yet.

A Background Repeating Timer in Swift | by Daniel Galasko, Background timers are a very usefool tool in one's developer toolbox. Problem is, NSTimer requires an active run loop which is not always readily If you try and resume/suspend an already resumed/suspended timer, you will get Porting Redux Architecture to Swift (Well, for a Toy macOS App Anyway). Countdown Timer Part 3 - KEEP TIMER RUNNING WHEN CLOSING THE APP (NO SERVICE) but if we close the app through the back button or by swiping it off the recent apps list, our timer stops and is

Be sure that you turn on the background mode and set needed values in xCode. It is strange, but even if background mode is turned off, this code works. It seems to be work any timers after setting application.beginBackgroundTask {} in AppDelegate.

I use this code:

In AppDelegate add code below:

func applicationDidEnterBackground(_ application: UIApplication) {

    application.beginBackgroundTask {} // allows to run background tasks

And call method below where you want.

func registerBackgroundTask() { .background).asyncAfter(deadline: + 5, qos: .background) {

Flutter keep app running in background, I'm trying to figure out a way to keep a Flutter application running even if it's not in But instead we will simply save the end-time of our CountDownTimer when More than two apps running - Smart background - Prevent suspension - Keep in � Create a Countdown Timer that counts down in seconds, minutes, hours and days to any date, with time zone support. It also counts up from a past date.

Try this instead.. this code updates the time when app comes back from background/suspend and in active state..

class ProgressV: UIView {

var timer: NSTimer!
var expirationDate = NSDate()

//Set the time that you want to decrease..
var numSeconds: NSTimeInterval = 36000.0 // Ex:10:00 hrs

func startTimer()
    // then set time interval to expirationDate…
    expirationDate = NSDate(timeIntervalSinceNow: numSeconds)
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(updateUI(_:)), userInfo: nil, repeats: true)
    NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

func updateUI(timer: NSTimer)
  // Call the currentTimeString method which can decrease the time..
   let timeString = currentTimeString()
   labelName.text = timeString
func currentTimeString() -> String {
    let unitFlags: NSCalendarUnit = [.Hour, .Minute, .Second]
    let countdown: NSDateComponents = NSCalendar.currentCalendar().components(unitFlags, fromDate: NSDate(), toDate: expirationDate, options: [])

    var timeRemaining: String
    if countdown.hour > 0
        timeRemaining = String(format: "%02d:%02d:%02d", countdown.hour, countdown.minute, countdown.second)
    else {
        timeRemaining = String(format: "%02d:%02d:%02d", countdown.hour, countdown.minute, countdown.second)
    return timeRemaining


How to run a timer in background within your iOS app, If you wish to run a timer in background within your iOS Application, Apple provides beginBackgroundTaskWithExpirationHandler method, you� Download this app from Microsoft Store for Windows 10, Windows 8.1, Windows 10 Mobile, Windows Phone 8.1. See screenshots, read the latest customer reviews, and compare ratings for Timer Countdown.

Ios Background Task, The podcast app in our example can make use of the API to continue If you want suspended apps to check for new content, go to Settings > General How to run a timer in background within your iOS app; How to create a high resolution timer with C++ and Linux? How to make a countdown timer in Android? Description� You may also like: 7 Epic countdown timer apps for Android & iOS. back to menu ↑ Visual Timer. Visual Timer is a great app for those who like pretty pictures. The timer is gorgeous. It can be used in many different ways – to remind you when to work when to rest when to eat, and when to sleep.

‎Count down to your next event - your next vacation, your favorite band’s concert, the next movie premiere, your child's graduation, your anniversary, and more - in style! Features: • WIDGET: Quickly view your Countdowns in real-time with iOS 8! Just tap “Edit” in the Notification Center and add Co…

Setup multiple countdown timers for tracking activities. Timers use scheduled toast notification which enable notifications to happen in background even if app is not running. Features such as Linked timers, which allow for timers sequences and Voice notification required app to be running in foreground.

  • with this method is easy to change the device date and hack the countdown, how is possible to do without hack?
  • Short answer: It's not possible. The app does not get time when it's suspended, and you depend on the system clock to calculate the amount of time elapsed since your code was last executed. You can ask for notifications when the clock changes, and you could probably use that notification to figure out that the user has messed with the clock.
  • thanks! good to know that being in background means using full power. I'll be using the solution suggested by Roland
  • Yup. He and I suggested the same approach. Looks like he posted his answer a little sooner than I did though.
  • Try this instead.. this code updates the time when app comes back from background/suspend and in active state..