8

I need to implement event when i close my app in iOS like following pic.

enter image description here

I want to catch that event when i close app with that minus button.

Which event i need to do?

Fire Fist
  • 7,032
  • 12
  • 63
  • 109

4 Answers4

33

This delegete will be called when your application is going to terminate.

- (void)applicationWillTerminate:(UIApplication *)app
{
}

If you are using iOS 4 or greater and with multi-tasking support, applicationWillTerminate will not be called.

For receiving the terminate event you need to add UIApplicationExitsOnSuspend key on your info.plist

Check this article for a good understanding about application events.

Without mutitasking

With mutitasking

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • 4
    If you use `UIApplicationExitsOnSuspend` without good reason I will 1-star-vote your app. And there are no good reasons for apps that are developed today. `UIApplicationExitsOnSuspend` is a major annoyance. – Matthias Bauch Jan 12 '13 at 09:25
  • @MatthiasBauch: ya you are right :) But for receiving the terminate event call i iOS 4 or greater there is no way other than this :( – Midhun MP Jan 12 '13 at 09:28
  • 1
    Even with `UIApplicationExitsOnSuspend` you can't capture the event that happens when the user "force closes" your app in the multitasking bar. The app will be terminated when the user taps the home button. – Matthias Bauch Jan 12 '13 at 09:32
  • For apps 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 app. For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason. – Julius Sep 05 '17 at 15:12
  • As mentioned above, the system MAY call applicationWillTerminate: when the system needs to terminate the app when it is RUNNING in background. – Julius Sep 05 '17 at 15:13
3

You may want to implement the

- (void)applicationWillTerminate:(UIApplication *)app;

method in your app delegate.

(Documentation)

3

You can handle it in

    - (void)applicationWillTerminate:(UIApplication *)application

which tells the delegate when the application is about to terminate.

Alternatively, you can listen to to the UIApplicationWillTerminateNotification notification.

See also: UIApplicationDelegate Protocol Reference

Attila H
  • 3,616
  • 2
  • 24
  • 37
1

func sceneDidDisconnect(_ scene: UIScene) if you're using SceneDelegate

Oded Breiner
  • 28,523
  • 10
  • 105
  • 71