Unfortunately the iPhone doesn't do very much to help you here. I can't guarantee that this will be useful for you but this is how I do it.
In my app I have the following protocol:
@protocol SaveState
- (NSData*) saveState;
- (id) initWithSaveState:(NSData*)data;
@end
Any UIViewController
that I need to be able to save its state implements it.
In applicationWillTerminate:
I have the following code:
for (UIViewController* vc in self.navigationController.viewControllers) {
if ([vc conformsToProtocol:@protocol(SaveState)]) {
NSArray* state = [NSArray arrayWithObjects:NSStringFromClass([vc class]), [(UIViewController<SaveState>*)vc saveState], nil];
[vcList addObject:state];
}
}
I then save vcList
to the NSUserDefaults
. To restore the state I have this in applicationDidFinishLaunching:
:
for (NSArray* screen in screenList) {
UIViewController<SaveState>* next = [[NSClassFromString([screen objectAtIndex:0]) alloc] initWithSaveState:([screen count] == 2) ? [screen objectAtIndex:1] : nil];
if (next != nil) {
[[self navigationController] pushViewController:next animated:NO];
[next release];
}
else {
// error handling
}
}