VladyslavPG
VladyslavPG

Reputation: 577

Timer in Background in Swift 2

I want to count costumer time(like this 00:20:04). But background app can work only 3 min. I have timer for it, and I can count with NSDate how many seconds app be close. But don't know how call this func.

So my question is: How app can understand when app in background and stop work, and how understand when return to app?

Upvotes: 0

Views: 692

Answers (1)

Nick Allen
Nick Allen

Reputation: 1873

Short answer:

Save current time when user start counting and calibrate it in applicationWillEnterForeground: , in fact you can just calibrate it in your code of setting up timer.

Long answer:

You won't receive any notification if your app is in suspend mode and then terminated.

Apps must be prepared for termination to happen at any time and should not wait to save user data or perform other critical tasks. System-initiated termination is a normal part of an app’s life cycle. The system usually terminates apps so that it can reclaim memory and make room for other apps being launched by the user, but the system may also terminate apps that are misbehaving or not responding to events in a timely manner.

Suspended apps receive no notification when they are terminated; the system kills the process and reclaims the corresponding memory. If an app is currently running in the background and not suspended, the system calls the applicationWillTerminate: of its app delegate prior to termination. The system does not call this method when the device reboots.

In addition to the system terminating your app, the user can terminate your app explicitly using the multitasking UI. User-initiated termination has the same effect as terminating a suspended app. The app’s process is killed and no notification is sent to the app.

However you can use these method of AppDelegate to get notified when your app is launched, entering background or foreground:

application:willFinishLaunchingWithOptions:—This method is your app’s first chance to execute code at launch time.

application:didFinishLaunchingWithOptions:—This method allows you to perform any final initialization before your app is displayed to the user.

applicationDidBecomeActive:—Lets your app know that it is about to become the foreground app. Use this method for any last minute preparation.

applicationWillResignActive:—Lets you know that your app is transitioning away from being the foreground app. Use this method to put your app into a quiescent state.

applicationDidEnterBackground:—Lets you know that your app is now running in the background and may be suspended at any time.

applicationWillEnterForeground:—Lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active. applicationWillTerminate:—Lets you know that your app is being terminated. This method is not called if your app is suspended.

read iOS app life cycle and UIApplicationDelegate Protocol Reference for more detail.

Upvotes: 1

Related Questions