title: Continue timer when iOS app is terminated
body: <p>How can I continue running my timer when my app is terminated from multitasking?</p>
  
  <p>My timer continues normally when app is closed then re-opened but when it is terminated the timer continues to run but doesn't account for the time since the app was killed.</p>
  
  <p>My timer Looks like this :</p>
  
  <pre><code>-(void)beginTimer {
  //hide/unhide stuff
  punchIn.hidden = 1;
  hhmmssLabel.hidden = 0;
  punchOut.hidden = 0;
  breakTime.hidden = 0;
  
  hhmmssLabel.text = @"00:00:00";
  
  
  //timer function
  [self unPauseTimer];
  
  pauseTimeInterval = 0;
  
  [[NSUserDefaults standardUserDefaults] setBool:timerIsRunning = YES forKey:@"running"];
  [[NSUserDefaults standardUserDefaults] synchronize];
  [[NSUserDefaults standardUserDefaults] setBool:onBreak = NO forKey:@"break"];
  [[NSUserDefaults standardUserDefaults] synchronize];
  [[NSUserDefaults standardUserDefaults] setDouble:pauseTimeInterval forKey:@"pausedTime"];
  [[NSUserDefaults standardUserDefaults] synchronize];
  
  
  }
  -(void)endTimer {
  //hide/unhide stuff
  punchIn.hidden = 0;
  hhmmssLabel.hidden = 1;
  punchOut.hidden = 1;
  breakTime.hidden = 1;
  
  [[NSUserDefaults standardUserDefaults] setBool:timerIsRunning = NO forKey:@"running"];
  [[NSUserDefaults standardUserDefaults] synchronize];
  
  //timer function
  [self pausedTimer];
  
  [self addToPlist];
  
  second = 0;
  pauseTimeInterval = 0;
  
  }
  
  //timer method from RMD
  -(void) updateTimer {
  
  NSDate *currentDate = [NSDate date];
  NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
  second = (NSUInteger)round(timeInterval);
  NSString *string = [NSString stringWithFormat:@"%02lu:%02lu:%02lu",
                      second / 3600, (second / 60) % 60, second % 60];
  hhmmssLabel.text = string;
  pauseTimeInterval = timeInterval;
  
  [[NSUserDefaults standardUserDefaults] setDouble:pauseTimeInterval forKey:@"pausedTime"];
  [[NSUserDefaults standardUserDefaults] synchronize];
  
  
  }
  -(void)pausedTimer {
  
  [tickerTimer invalidate];
  tickerTimer = nil;
  }
  
  -(void) unPauseTimer {
  
  startDate = [NSDate date];
  startDate = [startDate dateByAddingTimeInterval:((-1)*(pauseTimeInterval))];
  [[NSUserDefaults standardUserDefaults] setObject:startDate forKey:@"startDate"];
  [[NSUserDefaults standardUserDefaults] synchronize];
  tickerTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
  
  }
  </code></pre>
  
  <p>In my appDelegate i have this:</p>
  
  <pre><code>- (void)applicationWillTerminate:(UIApplication *)application
  {
  // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  
  [[ViewController sharedLayer] saveDataOnKill];
  }
  </code></pre>
  
  <p>which calls this:</p>
  
  <pre><code>-(void)saveDataOnKill {
  
  [[NSUserDefaults standardUserDefaults] setBool:timerIsRunning = YES forKey:@"running"];
  [[NSUserDefaults standardUserDefaults] synchronize];
  [[NSUserDefaults standardUserDefaults] setBool:onBreak = NO forKey:@"break"];
  [[NSUserDefaults standardUserDefaults] synchronize];
  [[NSUserDefaults standardUserDefaults] setDouble:pauseTimeInterval forKey:@"pausedTime"];
  [[NSUserDefaults standardUserDefaults] synchronize];
  }
  </code></pre>
  
  <p>I know somehow the timer knows where it left off and continues but it doesn't account for the time since the app has been killed.</p>