Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse - Bypass Login in AppDelegate

I am working to bypass login screen if the current user is not nil. To do this, I followed the recommendation posted here. After running the code, I get a blank white view in the simulator without any errors showing. Here is the code:

 storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle());
        let currentUser = PFUser.currentUser()
        print(currentUser)
        if currentUser != nil {
            self.window?.rootViewController = storyboard?.instantiateViewControllerWithIdentifier("HomeViewController");
        }

Worth mentioning that HomeViewController is based on Tab navigation. What am I missing?

The setup I have is:

  • App is launched and the first view is the Sign In view (with button for signup). This initial view is SignInViewController and I embedded a UINavigationController to it.
  • User can login by providing credentials (if correct, segue is performed to redirect to HomeViewController)
  • User can signup and follow the steps (multiple controllers). At the end they will be redirected to Home View Controller.
like image 559
ksa_coder Avatar asked Apr 28 '26 09:04

ksa_coder


1 Answers

I'm assuming your home tab bar controller has the identifier "HomeViewController" and your sign in view controller has the identifier "SignInViewController" and this code is in your didFinishLaunchingWithOptions() AppDelegate method:

storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
if let currentUser = PFUser.currentUser() { // currentUser is not nil
    // set up app for valid in user
    self.window?.rootViewController = storyboard?.instantiateViewControllerWithIdentifier("HomeViewController")
    return true // don't load the rootViewController from your storyboard (if you have an initial view set)
} else { // there is no current user
    // set up app for new or non logged in user
    self.window?.rootViewController = storyboard?.instantiateViewControllerWithIdentifier("SignInViewController")
    return true
}

From your comments, it sounds like you have a navigation controller with its root view controller being your sign in view controller, which is why when you segue to your tab bar controller, you get a navigation bar.

Since with my approach you're not getting the segue (and the sign in view controller's navigation bar), initialize a new UINavigationController and set its rootViewController to be your tab bar controller:

if let currentUser = PFUser.currentUser() { // currentUser is not nil
    // load tab bar controller
    let tabBarController = storyboard?.instantiateViewControllerWithIdentifier("TabBarViewController")

    // initialize a new navigation controller with the tab bar controller as its rootViewController
    let navC = UINavigationController(rootViewController: tabBarController)

    // set the window's root view controller to be your new navigation controller
    self.window?.rootViewController = navC
}
like image 79
JAL Avatar answered May 03 '26 22:05

JAL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!