I need to save my NSUserDefault
when my app is completely quit in iOS.
not didenterbackground
and foreground
.
Only when user kill my app with minus(-) sign in iOS.
So i write following codes in applicationWillTerminate
of AppDelegate
.
- (void)applicationWillTerminate:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"iCloud"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
But it doesn't save to NSUserDefault
.
So where should i write to save my data?
The problem is that
applicationWillTerminate
is not getting called at all.Change your
applicationDidEnterBackground
as below and kill your application within 20 seconds. You can seeapplicationWillTerminate
getting called & setting youriCloud
value properly inNSUserDefaults
You can set your
Application does not run in background" = "YES"
in your info.plist file and you can seeapplicationWillTerminate
delegate getting called & storing values inNSUserDefaults
.When an app is ended from the multitasking bar, is is simply killed. There is no delegate method called.
willTerminate
is called for apps that do not support multitasking when the home button is hit.Apps by default support multitasking, so you should be doing everything you need to do in didEnterBackground. After that, you can't guarantee that any of your code will be called again.
From Apple Doc :
It's not necessary that
applicationWillTerminate
will be called when app exits, it may not be called when your app supports background execution.You can use UIApplicationExitsOnSuspend to specifies that the app should be terminated rather than moved to the background when it is quit.
I afraid, there isn't any delegate method which will surly handle app's force-quite scenario.
Hope this may help you in solving your problem.