title: Swift: Timer run in foreground/background
body: <p>I'm learning how to create a Pomodoro app, and am able to send notifications. However, I am totally clueless as to how to allow my timer label to update itself on reloading the app. Which means the timer works only when the app is open and not when it's in the foreground/background. Hoping to find a tutorial to learn from or just a quick answer code. Thanks!</p>
  
  <p>Edit: Just to clear some misunderstandings, my app's Notification works fine with the timer, for example if 30mins is selected, the app would notify the user after 30mins. However, the problem is that when the app reopens, it resumes for example 29:57 seconds left on the timer label while the 30mins should have passed already.</p>
  
  <pre><code>*Added in AppDelegate*
  var seconds = 0 //Timer countdown seconds
  var currentDate = NSDate()
  var setDate: Int = 0
  
  func pauseApp(){
      viewC.timer.invalidate() //invalidate timer
      UserDefaults.standard.set(seconds, forKey: "current") //error occurs here where "Cannot assign value of type NSDate to type Timer"
      setDate = UserDefaults.standard.integer(forKey: "current")
  }
  func startApp(){
      let difference = currentDate.timeIntervalSince(NSDate() as Date) as Double
      seconds = Int(Double(setDate) - difference)
      viewC.updateTimer()
  }
  </code></pre>
  
  <p>What someone suggests from a different thread is cancel the timer and store a NSDate when the app goes to the background. He stated we can use this notification to detect the app going to the background:</p>
  
  <pre><code>NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseApp", name: UIApplicationDidEnterBackgroundNotification, object: nil)
  </code></pre>
  
  <p>Then cancel the timer and store the date:</p>
  
  <pre><code>func pauseApp(){
  self.stop() //invalidate timer
  self.currentBackgroundDate = NSDate()
  </code></pre>
  
  <p>}
  Use this notification to detect the user coming back:</p>
  
  <pre><code> NSNotificationCenter.defaultCenter().addObserver(self, selector: "startApp", name: UIApplicationDidBecomeActiveNotification, object: nil)
  </code></pre>
  
  <p>Then calculate the difference from the stored date to the current date, update your counter and start the timer again:</p>
  
  <pre><code>func startApp(){
  let difference = self.currentBackgroundDate.timeIntervalSinceDate(NSDate())
  self.handler(difference) //update difference
  self.start() //start timer }
  </code></pre>
  
  <p>However, I do not fully understand this code (namely, the difference between the "handler" and my own "seconds") as am new to programming... Hoping for an answer or helpful insight. </p>
  
  <hr>
  
  <p>Solved: I managed to solve it myself from this video <a href="https://www.youtube.com/watch?v=V6ta24iBNBQ" rel="nofollow noreferrer">https://www.youtube.com/watch?v=V6ta24iBNBQ</a>
  Using this concept of timeDifference as well as UserDefaults.standard.set....
  I managed to adapt it to my personal <a href="https://itunes.apple.com/us/app/bromodoro/id1286503838?mt=8" rel="nofollow noreferrer">app</a> with the code</p>