Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

restoring iOS app to original state and clearing all data when user logs out

i have an app with a signin viewController which modally presents a tabBarController containing several tabs. Each tab has a navigationController and a stack of views. One of those tabs is for settings and has a logout button.

when the user presses the logout button I would like to dismiss all navigation stacks of all tabs and the tabBarController and go back to the initial login viewController. Essentially I want to restore the app to the initial state. Was wondering what the best practice would be to achieve this.

thanks

like image 200
alionthego Avatar asked Dec 21 '25 18:12

alionthego


2 Answers

If you want to reset all of the tabs and return the apps to it's initial state after logging out, all you have to do is reset the UITabBarController's viewControllers property.

So if you are subclassing UITabBarController the following code should restore your app to its original state.

    self.viewControllers = @[self.viewControllerOne, self.viewControllerTwo, self.viewControllerThree];

From the documentation:

If you change the value of this property at runtime, the tab bar controller removes all of the old view controllers before installing the new ones. The tab bar items for the new view controllers are displayed immediately and are not animated into position.

like image 167
jstn Avatar answered Dec 23 '25 07:12

jstn


This is the code I currently use in the app I am working on to move between the "Login" and "Main UI"

let toViewController = // Login view controller here
let fromView = UIApplication.sharedApplication().keyWindow!.rootViewController!.view
UIApplication.sharedApplication().keyWindow?.rootViewController = toViewController

let toView = toViewController.view
toView.addSubview(fromView)

UIView.animateWithDuration(0.38, delay: 0.2, options: [], animations: {
    fromView?.transform = CGAffineTransformMakeTranslation(-UIScreen.mainScreen().bounds.width, 0)
}) { finished in
    fromView.removeFromSuperview()
}
like image 34
DBoyer Avatar answered Dec 23 '25 09:12

DBoyer