How to save application state using NSUserDefaults on iPhone?

That's what I'm doing:

- (void) applicationWillTerminate: (UIApplication*) application {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:navigationController.viewControllers
        forKey:@"diabetesstate"];
}

- (void) applicationDidFinishLaunching: (UIApplication*) application {
    NSMutableArray *state = [[NSUserDefaults standardUserDefaults] 
        objectForKey:@"diabetesstate"];
    if (state == nil) {
        //Will initialize a new controller and put it as root view controller
    }
    else {
        [navigationController setViewControllers:state animated:NO];
    }
}
+3
source share
1 answer

Looks like you're trying to add a UIViewController to userdefaults? I doubt it will work.

I assume that you will need to put some line or identifier number where it indicates which view manager is currently displayed, and when the application starts to basically check this value and configure your view controllers accordingly.

- . , , . - , , NULL. , . NULL, , , userdefaults.

+1

Source: https://habr.com/ru/post/1748961/


All Articles