Run a timer in the background when the application is closed

I need to start the timer in the background when my application is closed, however I read that this is not possible. I know that I can run it in the background for ~ 10 minutes, but I need much more (up to 2 hours).

I was wondering if there is a way to do this, keeping the timer value when the application is closed, the current time, retrieving them when re-opened and comparing them with the current time to get the difference. Then I can restart the timer and add the values ​​together.

Anyone have any suggestions on how I can do this?

Thank!

+1
ios swift nstimer
Jul 26 '15 at 20:45
source share
1 answer

The right approach is to maintain the illusion that the timer works without actually supporting it. There are two aspects to this:

  • Before the application leaves the foreground, it must save the timer information (for example, either the start time of the timer if you are counting, or the time during which the timer should stop if you count), you can use various technologies to save this information (e.g. plist, keyed archiver, Core Data, etc.) in persistent storage.

    Then, when the application starts up again, check the saved time and restore the user interface to restart the timer from the corresponding state.

  • If you are looking for some time (i.e. you want to tell the user when a specific point in time has been reached, even if the application is not running), you can use local notification, see the Local and Remote Notification Programming Guide , with a focus on local notifications .

+1
Jul 26 '15 at 21:14
source share



All Articles