How to know the termination of app?
I added this code in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification
object : nil];
and if the app ends, it will notify me of termination.
- (void)applicationWillTerminate:(NSNotification*) notif{
NSLog(@"program will end");
}
But it doesn’t work…
I terminated the app, by clicking home button and pressing home button in 2sec, followed by clicking app icon’s ‘-‘button.
I want to be notified of the termination of app.
And also intereseted in what function to be called when the app terminates.(is it viewDidLoad?)
And the termination of app by clicking the ‘-‘button is to send the app SIGKILL?
If you press the home button, your app will be sent to the background. When you then kill it (by pressing the
-button), it likely does not get the notification because it is not running anymore.applicationWillTerminateis called on iOS < 4.0, when no multitasking (background) is available or whenUIApplicationExitsOnSuspendis set in the info.plist file.On iOS 4.x, when your app is sent to the background, it receives
applicationWillEnterForeground:. Look at the UIApplicationDelegate Protocol Reference for more info:Keep in mind that you need to do this both in
applicationWillEnterForegroundandapplicationWillTerminateif you also support iOS < 4Have a look also at this post from S.O.