timer

I've created a simple count down timer app in Swift. It works great except when i minimize the app, the timer stops ( and starts again immediatly again when i bring the app back up). I want the timer to continue ( even if minimized) and execute an action. Does anyone have a suggestion??

Thank you..

Replies

You can have the timer run in background for some time :

h ttp://zaachi.com/2013/08/09/ios-running-a-timer-within-your-app-is-in-the-background.html


Another way is to get the time when you enter background, keep this value as well as timer value

var tEnterBackground
var timerValueWhenEnterBackground


When app returns to foreground, read time again

var newTime


Then adjust timer accordingly

let elapsedTime = newTime - timerValueWhenEnterBackground
timerValue = timerValue + elapsedTime

Running a timer in the background seems easy and a nice solution. Unfortunately, I also had to find out that this is not possible. Every app has a certain background time, but this is only short and can be implemented, for example, with applicationDidEnterBackground when the app goes into the background. Only this function is carried out only a few seconds after the app goes into the background. It is also possible to extend this background time, but not to an unlimited period. The solution would be to save with userDefaults when the app goes to the background and then to calculate the elapsed time when it goes back to the foreground (applicationDidEnterForeground). Best regards For more information just search the internet