1

How can I know whether app is terminated by user?

(Double-clicking Home button and press red minus '-' button)

I need sample or pseudo code...

ChangUZ
  • 5,380
  • 10
  • 46
  • 64
  • possible duplicate of [How to know whether app is terminated by user or iOS (after 10min background)](http://stackoverflow.com/questions/7343404/how-to-know-whether-app-is-terminated-by-user-or-ios-after-10min-background) – Ali Oct 27 '14 at 05:08

4 Answers4

2

Sometimes you do get the UIApplicationWillTerminateNotification (you can also implement the applicationWillTerminate: method in your app delegate), but it's not guaranteed that you do get any notification at all in this case. So don't rely on it.

You won't receive the notification if your app is "suspended", that is it is in background but not running. In the suspended case you simply get killed with the non-catchable SIGKILL. Same for when you get killed due to out-of-memory.

From the documentation for applicationWillTerminate: (emphasis mine):

For applications that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the application. For applications that support background execution, this method is generally not called when the user quits the application because the application simply moves to the background in that case. However, this method may be called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • Why? I think it is guaranteed. But it is not guaranteed if you are stopping your app in Xcode. – Nekto Sep 22 '11 at 08:01
2

Try this:

- (void)applicationWillTerminate:(UIApplication *)application
{
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}

You should implement the method above in your class that implements UIApplicationDelegate

Some more notes: that method won't be called if you are stopping your app via Xcode (stop debug button). So be patient when testing.

Nekto
  • 17,837
  • 1
  • 55
  • 65
  • 2
    This method is not called. I checked console (Organizer-Devices) – ChangUZ Sep 22 '11 at 08:06
  • NSLog(@"_____Appliction will term-."); is not called. I put this code in applictaionWillTerminate method. – ChangUZ Sep 22 '11 at 08:13
  • I always test after stopping debug mode. – ChangUZ Sep 22 '11 at 08:15
  • @Nekto: You are not guaranteed to receive this method in a multitasking app. You only get it when you're in the background but not yet suspended. – DarkDust Sep 22 '11 at 08:20
  • 1
    Thanx @DarkDust. To @ChangUZ I then advice to use method `- (void)applicationDidEnterBackground:(UIApplication *)application`... May be it will help you – Nekto Sep 22 '11 at 08:35
2

When user kills the app:

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

}

Application will enter inactive state:

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

User pushes Home button:

 - (void)applicationDidEnterBackground:(UIApplication *)application
{
  NSLog(@"Application Did Enter Background");
}
Youssef
  • 3,582
  • 1
  • 21
  • 28
  • Did you check applicationWillTerminate is called? – ChangUZ Sep 22 '11 at 08:07
  • I've tried killing the app from springboard and from XCode and it is not called, I think you will have to implement applicationDidEnterBackground to be sure your app does what you want. – Youssef Sep 22 '11 at 08:57
  • The only way that I managed to get this method to work is disabling "multitasking" in the app's Info.plist. That's the only way you can be sure that this method will be always called. However that is not a good solution, so simply do your tasks in applicationDidEnterBackground – Youssef Sep 22 '11 at 09:01
0

Also there is this:

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

}

Could be helpful if you want to know did it enter background ;)

Nekto
  • 17,837
  • 1
  • 55
  • 65
Lukas
  • 2,515
  • 4
  • 28
  • 37