1

Is there a way to perform some last actions when the user kills the application on iPhone?

In UIApplicationDelegate there is applicationWillTerminate: but as I understand it's not guaranteed to get called when the application terminates. Is there another way?

Ata01
  • 303
  • 4
  • 19
  • 2
    it will get called, but user exiting is not necessarily a termination. willResignActive will tell you about exits (terminating or not). – danh Mar 06 '13 at 23:46

4 Answers4

3

You can't rely on applicationWillTerminate being called. From the documentation:

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.

The proper place to save any state is when the app enters the background. Once that happens, there is no way to know if the app will return to the foreground or if it gets killed and then started from the beginning.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • `-applicationWillTerminate:` will also be called on the iPhone 3G (supporting it is tricky, but not impossible), and the docs also say "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". To avoid getting bitten when you add background tasks/etc, it's best to support both. – tc. Mar 07 '13 at 00:47
  • @rmaddy Does this answer stand true, even today? Facing some similar issue with http://stackoverflow.com/questions/42367803/local-banner-notification-for-terminating-app Where I want to show a notification only when the App is terminated. Any workaround? – sweta.me Feb 21 '17 at 13:38
  • @sweta.me My answer was a little out of date. Things have changed. I've updated the answer. – rmaddy Feb 21 '17 at 15:12
  • @rmaddy Thanks you for updating the answer. One quick question: what would you do if you want to have something performed ONLY WHEN the app gets terminated? Thank You once again. – sweta.me Feb 21 '17 at 15:59
  • 1
    @sweta.me You can't because there is no guarantee that your app will be notified when it gets terminated. – rmaddy Feb 21 '17 at 16:07
0

All methods concerning your app state are in your AppDelegate when you use one of the project templates.

Put the code in the applicationWillResignActive: method. It will get called if your app goes to an inactive state (terminating or no).

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
AndrejKolar
  • 284
  • 1
  • 9
0

The "correct" place to save state is in both -applicationDidEnterBackground: and -applicationWillTerminate:. Don't worry about double-saving; generally only one of them is called (IME, -applicationWillTerminate: is not called when your app is killed in the background).

Caveat: -applicationDidEnterBackground: is not guaranteed to be called, since it is called after your app enters the background (and thus becomes eligible for killing without notice!). If the device is low on memory when your app is backgrounded, it might be killed. The best way to avoid this is to not use too much memory in the first place.

You could use -applicationWillResignActive:, but I do not recommend this: apps become inactive quite frequently. An obvious is system dialogs (location/privacy prompts, Wi-Fi, notifications that show as alerts, alarms), TWTweetSheet, and I suspect MFMailComposeViewController, MFMessageComposeViewController, Notification Center, the app-switcher bar (e.g. to change tracks/enable orientation lock).

tc.
  • 33,468
  • 5
  • 78
  • 96
-1

you can use applicationWillResignActive method in the appdelegate, or you can do the following, if you want to save stuff, but for some reason, you dont want to do it in the app delegate:

- (void) viewDidLoad/init { 
    [[NSNotificationCenter defaultCenter]
        addObserver:self
        selector:@selector(myApplicationWillResign)
        name:UIApplicationWillResignActiveNotification 
        object:NULL];
}

- (void) myApplicationWillResign {
    NSLog(@"About to die, perform last actions");
}
Nitin Alabur
  • 5,812
  • 1
  • 34
  • 52