Save the current status when quit, auto-reload when re-open the app

前端 未结 2 940
刺人心
刺人心 2020-12-20 02:14

I\'m creating a simple app to simply save a person\'s name and a number associated with this person. I\'ve created a class for person like this:

@interface P         


        
相关标签:
2条回答
  • 2020-12-20 02:52

    You want to save to NSUserDefaults (documentation). Look at the UserDefaults example in the documentation.

    Also check out this question:

    How to register user defaults using NSUserDefaults without overwriting existing values?

    0 讨论(0)
  • 2020-12-20 02:53

    Easy to do with NSUserDefaults, here's some code to get you started:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        appDelegate.lastPerson.name = [defaults stringForKey:@"Name"];
        appDelegate.lastPerson.serial = [defaults stringForKey:@"Serial"];
    
        [window addSubview:[flipViewController view]];
        [window makeKeyAndVisible];
    }
    
    -(void)applicationWillTerminate:(UIApplication *)application {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setString:appDelegate.lastPerson.name forKey:@"Name"];
        [defaults setString:appDelegate.lastPerson.serial forKey:@"Serial"];
        [defaults synchronize];     
    }
    

    You can check if either of the strings are nil after loading which will indicate no stored value and you should then ask the user for input.

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