iOS - Logout functionality

7.1k views Asked by At

I have a logout button and I need the app to reset. Seems pretty standard but I don't see much information on how I would go about doing something like this. So far from what I've picked up I've manage to remove an NSUserDefaults object that represents the current user, I use FBSDKLoginManager().logOut() in case the user is logged via Facebook, and then I reset the root view controller to its initial view controller:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootViewController = HomeViewController()

appDelegate.window!.rootViewController = rootViewController
appDelegate.window!.makeKeyAndVisible()

However, I'm not sure if there's anything else to do. I saw one answer that said you need to loop through all the windows subviews and remove them, but is this still necssary when you change the rootViewController? Is there anything else that I'm missing? Also, as of now the home screen just appears when you click logout. Is there any way to make changing the root view controller an animation?

1

There are 1 answers

0
Max On BEST ANSWER

Most of the time if you change the root view controller other view controllers will be deallocated if they are no longer in the heirarchy. To check you can implement deinit in the view controllers that should no longer be in memory.

deinit {
  println("SomeViewController was deallocated")
}

And you can try this for the transition animation

UIView.transitionWithView(self.window, duration: 0.5, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {() -> Void in self.window.rootViewController = otherViewController}, completion: nil)