When will applicationWillTerminate be called? When will applicationWillTerminate be called? ios ios

When will applicationWillTerminate be called?


I have just explored this question (iOS 9.2). And I have got some results.

So, applicationWillTerminate is called when a user terminates the app without switching it to background mode: the app is active, the user makes double press on Home button and throws out the app.

But if a user switches the app to the background at first, and then after this tries to terminate the app, applicationWillTerminate will not be called.

You can check this:

- (void)applicationWillTerminate:(UIApplication *)application {    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"term"];    [[NSUserDefaults standardUserDefaults] synchronize];}

and

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"term"]){        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"term"];        [[NSUserDefaults standardUserDefaults] synchronize];        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"WORKED" message:@"term works" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];        [alert show];    }...return YES;

}


Starting in iOS 4, the practical answer is "never." (or at least "rarely".) You can't assume that it will ever be called. Normally what happens is that your app gets moved to the background when the user presses the home button and then a few seconds later it shifts to the "suspended state" (Still in memory but not receiving any CPU time.)

Once your app is in the suspended state the system can terminate it at any time without warning (usually due to memory pressure.)

When you are suspended you don't get the applicationWillTerminate call before being killed. You should assume that when you get a applicationDidEnterBackground: message, you are going to be suspended shortly after, and die while suspended. Get your affairs in order (save app state.)

You may still get calls to your applicationWillTerminate in certain cases, but you should not assume that you will.


App Termination according to the App Programming Guide for iOS:

  • 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.