State Preservation and Restoration issue in iOS using MMDrawer
Asked Answered
A

1

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.

Azriel answered 24/6, 2016 at 8:29 Comment(5)
not working as expected - so what's happening then? what code did you add? what debugging have you done? what have you observed?Broadleaf
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.Azriel
What do you mean with restoring state? Is this about logging in user?Articular
No, In my requirement, I have to restore some details screen.Azriel
Saving the state and simulate this when the app restarts is not an option?Banuelos
L
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
                    }
                }
Lapotin answered 12/2, 2018 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.