Run timer in 'background' when app is closed
I need to run a timer in the background when my app is closed, however I have been reading that this isn’t possible. I know that I can run it in the background for ~10 minutes, but I need considerably more than that (up to at least 2 hours).
I was wondering if there is a way to do this by saving the timer value when the app is closed, the current time, retrieving them when it re-opens, and comparing them to the current time to get the difference. I could then restart the timer and add the values together.
Anyone have any suggestions as to how I could do this?
- How to manage Enterprise Distribution certificate expiration?
- removing SCNNode does not free memory before creating new SCNNode
- asynchronous upload with NSURLSession will not work but synchronous NSURLConnection does
- How to manage data between the entities with the UIPICKERVIEW?
- iOS “the new iPad” UIDeviceHardware hw.machine codename
- Hide dots from UIPageViewController
Solutions Collect From Internet About “Run timer in 'background' when app is closed”
The correct approach is to maintain the illusion that the timer is running, without actually keeping it running. There are two aspects to this:
Before the app leaves the foreground, it should save information about the timer (e.g. either the time the timer started if you’re counting up or the time the timer is supposed to stop if you’re counting down). You can use a variety of different technologies to save this information (e.g. a plist, keyed archiver, Core Data, etc.) in persistent storage.
Then when the app starts up again, check the saved time and reconstruct the UI to start the timer again from the appropriate state.
If you’re counting down to some time (i.e. you want to let the user know when the particular point in time has been reached, even if the app isn’t running), you could use a local notification. See the Local and Remote Notification Programming Guide, focusing on the local notifications.
- Localizable.strings corrupted?
- Did Facebook recently disable accessing public page feeds via uri?
- write into plist file using NSDictionary object
- Multiple IBOutlets in same line of same type in Swift
- GenerateDSYMFile warning: unable to open object file
- Get Xcode 5 to warn about new API calls
- Call an Objective-C Method from Swift app
- iPhone / iOS : Presenting HTML 5 Keyboard for Postal Codes
- ios metal: limit on number of variables used in a shader
- IOS Facebook SDK: login doesn't return email despite permissions granted
- How do I print text onto the game screen
- How to send multiple parameterts to PHP server in HTTP post
- Detecting taps on attributed text in a UITextView in iOS
- enum's rawValue property not recognized
- Integrating Unity3D game into Android/iOS activity