What is the correct way to save NSUserDefaults whe

2019-03-29 12:05发布

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?

4条回答
何必那么认真
2楼-- · 2019-03-29 12:31

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;
        }
    });
}
查看更多
做个烂人
3楼-- · 2019-03-29 12:32

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.

enter image description here

查看更多
狗以群分
4楼-- · 2019-03-29 12:45

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.

查看更多
霸刀☆藐视天下
5楼-- · 2019-03-29 12:53

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.

查看更多
登录 后发表回答