I bet you don't need to start the timer in the background, you need to know the time elapsed during the suspension. It is much better to use your resources than trying to play an empty sound file.
Use applicationWillResignActive and applicationDidBecomeActive to determine how much time has passed. First save the firedate of your timer and invalidate it in applicationWillResignActive
func applicationWillResignActive(application: UIApplication) { guard let t = self.timer else { return } nextFireDate = t.fireDate t.invalidate() }
Then determine how much time is left for the timer in applicationDidBecomeActive by comparing the time with FireDate that you saved in the applicationWillResignActive call:
func applicationDidBecomeActive(application: UIApplication) { guard let n = nextFireDate else { return } let howMuchLonger = n.timeIntervalSinceDate(NSDate()) if howMuchLonger < 0 { print("Should have already fired \(howMuchLonger) seconds ago") target!.performSelector(selector!) } else { print("should fire in \(howMuchLonger) seconds") NSTimer.scheduledTimerWithTimeInterval(howMuchLonger, target: target!, selector: selector!, userInfo: nil, repeats: false) } }
If you need a repetitive, just do the math to find out how many times the timer had to fire in the background
let howManyTimes = abs(howMuchLonger) / repeatInterval
source share