10

I have integrated the MMDrawerController Library in an iOS application and now I have a requirement to Restore application state even though application is killed in background(Only when application is enter from foreground to background), it is working fine with normal navigation application but when I change Navigation using "setCenterViewController" option in my application, restoration is not working as expected and I have followed all instruction provided in this link: "https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/PreservingandRestoringState.html"

I have used setCenterViewController option(Recommended from MMDrawer) to navigates to particular screen and then deleted app in background , When we reopened it is launching with default initial screen but we are expecting it should reopen with Screen it has before Entering to Background.

and here is the code snippet:

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.homeController.navigationController.navigationBarHidden = YES;
HomeViewController *homeVC = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController *_navg = [[UINavigationController alloc]initWithRootViewController:homeVC];
_navg.restorationIdentifier = @"homeNavigationController";
homeVC.title = [self.dataSource objectAtIndex:indexPath.row]; homeVC.restorationIdentifier = @"HomeViewController";
[appDelegate.drawerController setCenterViewController:_navg withCloseAnimation:YES completion:nil];
self.currentViewController = _navg;
self.currentViewController.restorationIdentifier = @"homeNavigationController"; 

Help me to resolve this issue.

Brjv
  • 1,054
  • 1
  • 8
  • 24
  • 1
    `not working as expected` - so what's happening then? what code did you add? what debugging have you done? what have you observed? – Wain Jun 27 '16 at 08:59
  • I have used setCenterViewController option(Recommended from MMDrawer) to navigates to particular screen and then deleted app in background , When we reopened it is launching with default initial screen but we are expecting it should reopen with Screen it has before Entering to Background. – Brjv Jun 27 '16 at 09:44
  • What do you mean with restoring state? Is this about logging in user? – alicanbatur Jul 01 '16 at 07:47
  • No, In my requirement, I have to restore some details screen. – Brjv Jul 02 '16 at 14:15
  • Saving the state and simulate this when the app restarts is not an option? – thedp Dec 22 '16 at 15:30

1 Answers1

0

You can easily do state preservation by storing states in NSUUserDefault Do the following steps: 1.When you launch the app first time.

  {
      UserDefaults.standard.set("0", forKey: "state")
     UserDefaults.standard.synchronize()
   }

2.When you kill the app then store the state

   {    
            UserDefaults.standard.set("1", forKey: "state")
             UserDefaults.standard.synchronize()
        }

3. When you relaunch the app get the state ,setup the drawer and move to particular controller using it's navigation controller.



 if let state = UserDefaults.standard.object(forKey: "state") as? String{
         switch state{
                case "0":
                        //Do setup for MMDrawer for center ,left and right view
                        break
                case "1":
                        //Do setup for MMDrawer for center ,left and right view

                break
                 default:
                        break
                    }
                }