36

The applicationWillTerminate delegate method is not getting called in iOS 4.0 When I hit the Home button I am seeing the applicationWillResignActive and applicationDidEnterBackground delegate methods getting called.

 - (void)applicationWillResignActive:(UIApplication *)application
{
  NSLog(@"Application Did Resign Active");
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
  NSLog(@"Application Did Enter Background");
}

And when I double Tap the Home button and again launch the Application the i find the applicationWillEnterForeground and applicationDidBecomeActive delegate methods are getting called.

 - (void)applicationWillEnterForeground:(UIApplication *)application
{
  NSLog(@"Application Will Enter Foreground");
}



- (void)applicationDidBecomeActive:(UIApplication *)application
{
  NSLog(@"Application Did Become Active");
}

But I want to know when the applicationWillTerminate delegate method will be called , where I do some DB/file backup routines.

- (void)applicationWillTerminate:(UIApplication *)application{

}

I even tried to hit the minus sign and deleted the App running in the Background , but still it did not call any delegate method.

Any Ideas ???

Biranchi
  • 16,120
  • 23
  • 124
  • 161
  • You did actually put some code in `applicationWillTerminate:`, right? Your question currently shows an empty method body. – BoltClock Jun 29 '10 at 10:01
  • [here](http://stackoverflow.com/questions/7818045/applicationwillterminate-when-is-it-called-and-when-not) is the solution what i found so far – swiftBoy Apr 25 '13 at 11:19

4 Answers4

52

From the iPhone Application Programming Guide:

Even if you develop your application using iPhone SDK 4 and later, you must still be prepared for your application to be terminated. If memory becomes constrained, the system might remove applications from memory in order to make more room. If your application is currently suspended, the system removes your application from memory without any notice. However, if your application is currently running in the background, the system does call the applicationWillTerminate: method of the application delegate. Your application cannot request additional background execution time from this method.

So yes, applicationWillTerminate: will generally not be called very often in iOS 4. If you have to save data, you should do so in both applicationWillTerminate: and applicationDidEnterBackground:.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • 5
    Around minute 22 of the video quoted by falconcreek, it's said that only background running apps that are killed via the red minus icon get the benefit of a applicationWillTerminate: call before being killed. Suspended ones die right away. It's listed under 'Termination', and not made 100% clear, but it sounds like this applies to system termination and user initiated termination via the minus sign. – Joost Schuur Jun 29 '10 at 18:44
  • Running iOS 4 on a iPhone 3G it seems that applicationWillTerminate: gets called after applicationDidEnterBackground: when the home button is pressed. – David Barry Jan 12 '11 at 01:49
  • 3
    @David: well, the iPhone 3G does not support multitasking so that would make sense. – Ole Begemann Jan 12 '11 at 11:16
  • I know this thread is old but, how about iOS5? -applicationWillTerminate: method is not called even when I press the red minus button right after the app enter the background (pressed home button). Am I missing something? – nacho4d Nov 30 '11 at 06:14
  • @nacho4d: same behavior as iOS 4. – Ole Begemann Nov 30 '11 at 13:15
4

The WWDC 2010 Session Adopting Multitasking on iPhone OS (Part 2) explains the application state transitions extremely well.

Vincent Gable
  • 3,455
  • 23
  • 26
falconcreek
  • 4,170
  • 1
  • 21
  • 22
2

I got one solution for terminating apps when user hits the Home button in iOS4. This will call the applicationWillTerminate delegate method instead of entering into background process.

  1. Open your info.plist file
  2. Add The Key UIApplicationExitsOnSuspend
  3. Set the new key to YES
Biranchi
  • 16,120
  • 23
  • 124
  • 161
  • 4
    That will immediately exit your application instead of just suspending it. So when the user relaunches the app, it will have to start up all over again, eliminating the benefit of fast application switching. In general, using UIApplicationExitsOnSuspend is discouraged unless your application really needs to exit (http://tinyurl.com/2fgkf5p). – Brian Jul 16 '10 at 13:46
  • @Brian:There is no reason to discourage this behavior if it best fits the needs of your application and user base, otherwise there would be no point in the existence of the feature. – ninehundreds Sep 19 '12 at 16:38
  • @lakehousetech, you don't have to take my word for it. The iOS App Programming Guide says "Opting out of background execution is strongly discouraged but may be the preferred option under certain conditions." Which is pretty much what I said :-). The guide also says that apps that cold start should restore their previous state, so immediate termination is a rarely needed. – Brian Sep 19 '12 at 21:03
1

Actually you can also use this step to do so.

  1. Open your info.plist file
  2. Add The Key -> Application does not run in background
  3. Set this key value to YES
Zaldy Bughaw
  • 797
  • 8
  • 16