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;

}

Leave a Comment