Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to save NSUserDefaults when my app is terminated on iOS

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?

like image 520
Fire Fist Avatar asked Sep 22 '13 06:09

Fire Fist


1 Answers

From Apple Doc :

For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app. For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. 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.

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.

like image 188
Moin Ahmed Avatar answered Sep 21 '22 14:09

Moin Ahmed