0

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.

prabhu
  • 684
  • 11
  • 27
  • Please read the entire State Restoration guide here: https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/StatePreservation/StatePreservation.html#//apple_ref/doc/uid/TP40007072-CH11-SW13 - you are either missing lots of other parts, or you haven't included them in your question. – Aaron Brager Feb 24 '14 at 16:14
  • I am using JSidePanelController classes, when I debug I get that class as root view controller, so my above problem is because of panel controller ? – prabhu Apr 11 '14 at 18:17
  • I tried the above develpoer.apple link, but can somebody please tell me that in simple words so I can implement that ? I done simple demo of it and it works but not working in my project. – prabhu Apr 12 '14 at 07:34
  • You haven't provided sufficient information to diagnose the problem. Please describe your problem in more detail or upload a simple example that reproduces the issue. – Aaron Brager Apr 12 '14 at 16:33
  • Please check my updated question, I know that I am little weak to diagnose this problem, but please help me to come out of this problem. – prabhu Apr 12 '14 at 18:39
  • @AaronBrager, your link above is dead. Can you post another? – Heath Borders May 14 '15 at 16:45
  • can you confirm if it is solved? I'm having same issue. Its not working with xib screens – Hiren Prajapati Oct 12 '17 at 11:31

0 Answers0