I am trying to implement the state preservation and restoration mechanism in my app for that I implemented the following two methods in appDelegate also given the restoration id to each of my viewController in storyboard, the problem I am facing is, when I reopen the app I can see my previous or restored screen but for a second only, and then it moves to the Main storyboard file's base view controller.
- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
Please suggest me the solution for that, or if I am mistaking something here. Kindly waiting for reply. Thanks in advance.
Updated Part :
I am using following method in my app delegates NSString * const AppDelegateRootVCKey = @"AppDelegateRootVCKey";
- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder {
[coder encodeObject:self.window.rootViewController forKey:AppDelegateRootVCKey];
}
- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder {
// Grabs the preserved root view controller.
UIViewController * vc = [coder decodeObjectForKey:AppDelegateRootVCKey];
if (vc) {
UIWindow * window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = vc;
window.restorationIdentifier = NSStringFromClass([window class]);
// The green color is just to make it obvious if our view didn't load properly.
// It can be removed when you are finished debugging.
window.backgroundColor = [UIColor greenColor];
self.window = window;
}
}
Also my app initialise part is in willFinishLaunchingWithOptions and my didFinishLaunching is as follows
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window becomeFirstResponder];
return YES;
}
I given restoration identifier to all controller via storyboard and by code also to my other .xibs. So now result of above things is, whenever I was on storyboard screen it preserves my state but if I click on back or other button on header to open my left/right side menu screen, it is crashing, with reason as unrecognised selector. After crash if I reopen again it start from mainStoryboard and then everything works fine. Another problem is whenever I tested with my xib screen it does not preserve app state, and restart app from mainStoryboard.