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.
-
-applicationWillTerminate: will get fired. – borncrazy Apr 22 '14 at 13:48
-
1If the user force closes the app (flicking up in the app switcher) you will **not** get notified. – Rich Apr 22 '14 at 13:55
-
@borncrazy No it isn't. No method is called when an app is killed in the background. – rmaddy Apr 22 '14 at 15:30
2 Answers
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.

- 51,477
- 5
- 75
- 98
-
1Erm, won't `applicationWillTerminate:` be called when your app is going to be killed while in the background on low memory scenarios? From the docs: "*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.*" – Rich Apr 22 '14 at 14:04
-
1@Rich have you tried putting an NSLog in appWillTerminate to see if it gets called when the app is flicked up? I don't think it does. – Sam B Apr 22 '14 at 14:32
-
@SamBudda I know - see [this](http://stackoverflow.com/questions/23221398/which-method-called-when-app-killed-from-background/23221765?noredirect=1#comment35524909_23221398) :) I also clearly said in my above comment that it was "*low memory scenarios*" too! – Rich Apr 22 '14 at 14:36
At this time iOS call -applicationWillTerminate:
method you can get all the information about this method from official documentation of apple.

- 46,010
- 16
- 115
- 137
-
-
for check it.. you need to put alertView in this method, because your app in BG mode so, beak point does not work that time. – iPatel Apr 22 '14 at 13:50
-
2
-
No delegate method call if an app in background mode then terminates. – Nilesh R Patel Dec 30 '19 at 09:45