Which method called when app killed from backgroun

2019-09-17 16:55发布

问题:

This question already has an answer here:

  • Delete data when app is killed in the background. 3 answers

I made an application in that i save some variables in NSUserdefaults when app launches. But i want to do the thing that when app close from background then it must remove all the NSUserdefaults value. how can i do this. please help me.

回答1:

At this time iOS call -applicationWillTerminate: method you can get all the information about this method from official documentation of apple.



回答2:

On iOS, nothing gets called when your app gets killed while in the background.

At the point where you move from foreground to background, you are supposed to store all the information that is needed for your app to come back looking unchanged if it gets killed and launched again. By doing that, iOS has the freedom to kill your app at any time when in the background. This is necessary because iOS will kill you for example if it needs more memory for other apps; having to start your app again at that point would be totally counterproductive.

But what you are saying seems to indicate that you want the app to behave differently if killed in the background. That's the wrong thing to do. The user never should notice that your app was killed.