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 '-'butto开发者_开发知识库n.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?I terminated the app, by clicking home button and pressing home button in 2sec, followed by clicking app icon's '-'button.
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.
applicationWillTerminate
is called on iOS < 4.0, when no multitasking (background) is available or when UIApplicationExitsOnSuspend
is 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:
You should use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
Keep in mind that you need to do this both in applicationWillEnterForeground
and applicationWillTerminate
if you also support iOS < 4
Have a look also at this post from S.O.
That should work, did you try adding logging into the application delegate method applicationWillTerminate:
method too?
Edit:
For iOS 4+ devices, you need to do whatever work for shutdown in applicationWillEnterBackground instead... that always gets called as you are being suspended and is the right time to do final work.
you should implement applicationDidEnterBackground
and do any preparation for termination there. If your app is terminated in the background, you will receive no other notification.
精彩评论