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

后端 未结 4 919
不知归路
不知归路 2021-02-10 03:16

I need to save my NSUserDefault when my app is completely quit in iOS. not didenterbackground and foreground. Only when user kill my app w

相关标签:
4条回答
  • 2021-02-10 03:29

    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;
            }
        });
    }
    
    0 讨论(0)
  • 2021-02-10 03:40

    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

    0 讨论(0)
  • 2021-02-10 03:42

    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.

    0 讨论(0)
  • 2021-02-10 03:44

    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.

    0 讨论(0)
提交回复
热议问题