7

I'm working on a project in Xcode version 4.2.1 and in iOS 5.0. I'm wondering which function will be called when you completely terminate an application, which means even the time when an users force the app running on background to be terminated from home screen. I was assuming that

(void)applicationWillTerminate:(UIApplication *)application

would be called, but it turned out it was not called even after terminating the app on background. What I basically want to do is to call a method that I created just before the app is completely terminated, so need to know which function is called at the end of the app.

Thank you.

Max
  • 71
  • 1
  • 1
  • 3

5 Answers5

24

"applicationWillTerminate" method will be called only when application is not in suspended state while being terminated.

To understand this, considering currently your application is in foreground active state, go through following two cases:

Case 1: When you single tap Home button, app directly goes to suspended state by calling methods - (1) applicationWillResignActive and then (2) applicationDidEnterBackground.

Now when you try to quit/terminate app, by double tapping home button and then swiping up the current app screen from recent app screens, app is in suspended state. So, "applicationWillTerminate" method will not be called.

Case 2: When you double tap Home button, app goes to inactive state by calling method - (1) applicationWillResignActive.

Now when you try to quit/terminate app, by swiping up the current app screen from recent app screens, app is in inactive state (not in suspended state) while being terminated. So, "applicationWillTerminate" method will be called.

Look what Apple say: enter image description here

For more info on this look - Apple's Official Documentation on applicationWillTerminate(_:)

Nitesh Borad
  • 4,583
  • 36
  • 51
6

Here's a good overview of the application lifecycle notifications & delegate messages on iOS 4.0 and newer. In short...

Your app will generally never see willTerminate, because the system generally only terminates your app once it's already suspended (in the background). Once your app is suspended, it gets no further chance to act(*), so there's no callback for that.

The didEnterBackground delegate message or notification should be considered your last chance to clean things up or save state before possible termination.


(*) Okay, your app can do stuff if it's in one of the supported background execution modes, like audio, VoIP, or navigation, but in that case it either hasn't been suspended yet or it's been un-suspended with an entry point specific to that background mode.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • So in the case an user forcibly terminates the app running on background, there would be no good way to call my method. Thank you for your help. – Max Apr 18 '12 at 00:01
  • You probably won't get a notification even then; unless the user terminates your app via the app switcher within a couple of seconds of it going to the background, your app will already be suspended. – rickster Apr 18 '12 at 01:27
2

If the app is already suspended, it will not receive further notifications. Watch for the didEnterBackground notification.

Hiren
  • 12,720
  • 7
  • 52
  • 72
OldPeculier
  • 11,049
  • 13
  • 50
  • 76
  • Would you mean I cannot realize what I want to do as long as I'm in the home screen? As for the function, I didn't find it even though seeing such a kind of function. – Max Apr 17 '12 at 23:27
  • -1: There is no "willSuspend" notification or delegate message. You probably want "didEnterBackground". – rickster Apr 17 '12 at 23:35
  • I might not want it because I want to call a method when the application is completely terminated, not when pushing home button. That is, I don't want to call my method when just entering background. – Max Apr 17 '12 at 23:39
  • What do you want your method to *do* when the app is terminated? There might yet be a way to accomplish it. – rickster Apr 18 '12 at 01:30
  • My project makes use of sqlite3 and I want my method to delete all tables in a sqlite3 file at the end of the app because I don't want the sqlite3 file to keep data (tables) after terminating the app. – Max Apr 18 '12 at 01:46
0
- (void)applicationWillTerminate:(UIApplication *)application

It's in the UIApplicationDelegate protocol.

danh
  • 62,181
  • 10
  • 95
  • 136
0

The documentation here spells out the behavior for this case.

In short, you'll only receive the applicationWillTerminate: message if the app is running (either in the foreground or background) when it's terminated. So, unless your app is running in the foreground or in one of the long-running background modes (e.g. VoIP, Audio, or a long-running task indicated by beginBackgroundTaskWithExpirationHandler) it will transition from the background state to the suspended state fairly quickly. In this case, you'll never receive the applicationWillTerminate: message.

Art Gillespie
  • 8,747
  • 1
  • 37
  • 34