I'd like to implement state preservation and restoration in my iOS 6 app that doesn't use storyboards. My main view controller that I want to save state and restore is a UIViewController that's part of the UINavigationController.
My view controller extends UIViewControllerRestoration protocol. I think I'm implementing all of the necessary methods yet I don't see any calls to the encodeRestorableStateWithCoder or decodeRestorableStateWithCoder from the simulator.
Here's what my app delegate looks like: MyAppDelegate:
- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder {
NSLog(@"shouldSaveApplicationState"); // seeing this in the debug window
return YES;
}
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder {
NSLog(@"shouldRestoreApplicationState"); // seeing this in the debug window
return YES;
}
- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder {
NSLog(@"willEncodeRestorableStateWithCoder"); // seeing this in the debug window
}
- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder {
NSLog(@"didDecodeRestorableStateWithCoder"); // seeing this in the debug window
}
I see all of these calls in the debug window. Here's what my MyMainViewController, which is visible on app suspend, looks like:
@interface MyMainViewController : UIViewController <UIViewControllerRestoration>
@end
@implementation MyMainViewController
- (id)init {
if (self = [super init]) {
self.restorationIdentifier = @"MyViewControllerRestoreId";
self.restorationClass = [self class];
}
return self;
}
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:frame];
// Bunch of work to create my custom views
}
- (void)awakeFromNib {
self.restorationClass = [self class];
}
// Expected a call to this method, but not seeing it in debug window
+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents
coder:(NSCoder *)coder {
NSLog(@"viewControllerWithRestorationIdentifierPath: %@", identifierComponents);
MyMainViewController *vc = [[MyMainViewController alloc] init];
vc.restorationIdentifier = [identifierComponents lastObject];
vc.restorationClass = [MyMainViewController class];
return vc;
}
// Expected a call to this method, but not seeing it in debug window
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
NSLog(@"encodeRestorableStateWithCoder");
[super encodeRestorableStateWithCoder:coder];
}
// Expected a call to this method, but not seeing it in debug window
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
NSLog(@"decodeRestorableStateWithCoder");
[super decodeRestorableStateWithCoder:coder];
}
@end
The way I use the simulator to test preservation and restoration:
1. Launch app in Simulator via Xcode
2. Invoke the Home key so App is now suspended. This where I see:
shouldSaveApplicationState
willEncodeRestorableStateWithCoder
3. End the debugging session in XCode (Command .)
4. Start a new debug session of the app in XCode. I see:
shouldRestoreApplicationState
didDecodeRestorableStateWithCoder
I'm not sure what else I'm missing to make this work.