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

微笑、不失礼 提交于 2019-12-04 12:26:04

问题


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?


回答1:


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.




回答2:


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.




回答3:


You can set your Application does not run in background" = "YES" in your info.plist file and you can see applicationWillTerminate delegate getting called & storing values in NSUserDefaults.




回答4:


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 see applicationWillTerminate getting called & setting your iCloud value properly in NSUserDefaults

- (void)applicationDidEnterBackground:(UIApplication *)application
{

    __block UIBackgroundTaskIdentifier identifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        if (identifier != UIBackgroundTaskInvalid) {
            [[UIApplication sharedApplication] endBackgroundTask:identifier];
            identifier = UIBackgroundTaskInvalid;
        }
    }];

    dispatch_async(dispatch_get_main_queue(), ^{
        for (int i=0; i < 20; i++) {
            NSLog(@"%d", i);
            sleep(1);
        }
        if (identifier != UIBackgroundTaskInvalid) {
            [[UIApplication sharedApplication] endBackgroundTask:identifier];
            identifier = UIBackgroundTaskInvalid;
        }
    });
}


来源:https://stackoverflow.com/questions/18941008/what-is-the-correct-way-to-save-nsuserdefaults-when-my-app-is-terminated-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!