Let's say that my application state is extracted into an object (so that all information specific to app instance is contained in one object) and that object supports nscoding protocol. How can i easily persist it and load it on exit/launch of my application?
My current code looks like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
State* state = (State*)[defaults objectForKey:@"State"];
if(state!=nil)
{
viewController.state = state;
}
}
- (void)applicationWillTerminate:(UIApplication *)application {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.viewController.state forKey:@"State"];
}
But upon loading state is always nil... so I assume it is not the best pattern out there :)