What function is called when the iPhone application is terminated?

I am working on a project in Xcode version 4.2.1 and in iOS 5.0. I am wondering what function will be called when you completely complete the application, which means that even the time when users force the application running on the background to stop from the main screen. I assumed that

(void)applicationWillTerminate:(UIApplication *)application

but it turned out that it was not called even after the application terminated in the background. What I basically want to do is call the method that I created before the application is fully completed, so you need to know which function is called at the end of the application.

Thanks.

+6
source share
6 answers

The "applicationWillTerminate" method will only be called when the application is not in a suspended state at completion.

To understand this, given that your application is currently in an active foreground state, do the following two cases:

Case 1: When the button is a single Home button , the application directly goes into a suspended state by calling methods - (1) applicationWillResignActive, and then (2) applicationDidEnterBackground.

Now, when you try to close / close the application by double-clicking the home button, and then expanding the current application screen from the last application screens, the application is in a suspended state. Thus, the applicationWillTerminate method will not be called.

Case 2: When you double-click the Home button, the application goes into an inactive state by calling the method - (1) applicationWillResignActive.

Now, when you try to close / close the application by canceling the current application screen on the last application screens, the application is in an inactive state (not in a suspended state) when it is completed. Thus, the applicationWillTerminate method will be called .

See what Apple says: enter image description here

For more information about this image - Apple Official Documentation at applicationWillTerminate (_ :)

+18
source

Here's a good overview of app lifecycle notifications and message delegation on iOS 4.0 and later. In short ...

Your application, as a rule, will never see willTerminate , because the system usually terminates your application only after it is suspended (in the background). Once your application is paused, it will no longer be able to act (*), so there is no callback for this.

The message or notification to the didEnterBackground delegate should be seen as your last chance to clear things or keep state until possible.


(*) Well, your application can do anything if it is in one of the supported background modes, for example, audio, VoIP or navigation, but in this case it has either not been paused or was paused with an entry point specific for this background.

+5
source

If the application is already paused, it will not receive further notifications. Keep an eye on the didEnterBackground notification.

+1
source
 - (void)applicationWillTerminate:(UIApplication *)application 

This is in the UIApplicationDelegate protocol.

0
source

The documentation here describes the behavior for this case.

In short, you will receive a message applicationWillTerminate: if the application is running (in the foreground or in the background) when it is completed. Thus, if your application does not work in the foreground or in one of the long-running background modes (for example, VoIP, audio, or the long-term task indicated by the beginBackgroundTaskWithExpirationHandler symbol), it will switch from the background state to suspended quickly enough. In this case, you will never receive the applicationWillTerminate: message.

0
source

Three methods will be helpful:

  - (void)applicationWillResignActive:(UIApplication *)application { } - (void)applicationDidEnterBackground:(UIApplication *)application { } - (void)applicationWillTerminate:(UIApplication *)application { } 
-1
source

Source: https://habr.com/ru/post/913467/


All Articles