0

My app has the following flow if the user is logged in

Loading Screen -----> Main Screen -----> Rest of App

and the following flow if he's not :

Loading Screen -----> Login Screen -----> Main Screen -----> Rest of App

Now I am implementing the Logout feature. I have added the following code into main Screen :

func handleLogout() {
        if self.presentingViewController != nil {
            var vc = self.presentingViewController
            while ((vc!.presentingViewController) != nil) {
                vc = vc!.presentingViewController
            }
            vc?.dismissViewControllerAnimated(true, completion: {
            })
        }
}

This works fine if the 1st path is followed (the user was logged in when the app was launched) as the app returns to the Loading Screen and then loads up the Login Screen as expected. However, if the 2nd path was followed (the user was not logged in when the app was launched, and Login Screen has been used) this code leads to the Login Screen being opened directly and the whole logout process failing. Is there a way I can ensure that the Loading Screen is the one which is always loaded by this code regardless of which of the two paths have been followed.

1
  • 1
    Are you using storyboards? If so, simply create an unwind segue to loading screen and call that from wherever you need to Commented Aug 6, 2016 at 22:50

3 Answers 3

1

Use unwind segues!

You basically add an unwind segue connecting your "main screen" and "login screen". Give it an identifier and you can initiate the segue whenever you want. In handleLogout:

func handleLogout() {
    self.performSegueWithIdentifier("your identifier", sender: self)
}

For details of how to create an unwind segue: https://www.andrewcbancroft.com/2015/12/18/working-with-unwind-segues-programmatically-in-swift/

Sign up to request clarification or add additional context in comments.

Comments

0

This is just a suggestion here goes:

In AppDelegate file you can do something similar to this:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //Implement this method
        let userLoggedIn = isUserLoggedIn();
        if !userLoggedIn {
            let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
            //Instantiate the login view controller
            window?.rootViewController = storyboard.instantiateViewControllerWithIdentifier("login")
            window?.makeKeyAndVisible()
        }

        return true
    }

Now when the app starts it'll first check the user's login state and display the appropriate view.

Note: this is assuming you use storyboards and the root view controller is set to the Main Screen

Comments

0

If you are using Storyboards, I would suggest creating storyboard that is used purely for your login view/s. Then in the AppDelegate DidFinishLoading method, you can show the login storyboard if they need to login or show the main storyboard if they are already logged in. You can swap out storyboards at anytime and its easy to do. That will help simplify the flow a bit. This is what I usually do in my apps. If you need sample code just let me know.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.