Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save Application State using NSUserDefaults in iPhone?

Following is what i am 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];
    }
}
like image 844
keith Avatar asked Nov 30 '25 04:11

keith


1 Answers

It looks like you're trying to add a UIViewController to the userdefaults? I doubt that will work.

I guess you'll have to put some identifer string or number in there that tells you which viewcontroller is currently displayed, and when the application starts basically check that value and set up your viewcontrollers accordingly.

I need to implement something similar for my application. I've got a list of objects, and when the user taps on one I am displaying a child object. My idea is to store the ID of the client object if one is currently being displayed, otherwise NULL. When the application starts I will check the value. If it's NULL I'll display the list of parent objects, otherwise I'll show the child object with the ID that's in userdefaults.

like image 146
Thomas Müller Avatar answered Dec 02 '25 19:12

Thomas Müller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!