I am trying to verify if there is a user before deciding whether to display the login/signup view controller or the logged in view controller. The issue I am running into is that to check for the presence of a user, I have to wait for viewDidAppear
, since for some reason the user is always nil
if I run the check in viewDidLoad
.
I am using Parse for my user system, but I'm not sure if that is part of the issue. The below code works fine, except the view for ViewController
gets loaded for a split second right before the if(PFUser.currentUser() != nil)
line gets executed.
I want to be able to run the user check in viewDidLoad
so the view doesn't have to actually appear, but the user always return nil
if I do that.
I've tried adding a blank view controller before the login/signup screen, but that just loads a white screen for a second. I tried doing it in the storyboard launch screen but that's not allowed (for good reason I believe), and I could create a view that is a copy of the launch screen and run it from there, but that seems kind of like a hack.
What is the proper method for doing this? I assume this is something that is commonly done among most apps, I just can't figure out where to execute that if
statement.
import UIKit
import Parse
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if (PFUser.currentUser() != nil) {
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("summaryView")
self.presentViewController(vc, animated: true, completion: nil)
}
}
}
You can put your code in application(_:, didFinishLaunchingWithOptions:)
.
if let user = PFUser.currentUser() {
// Code to execute if user is logged in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("VCIdentifier")
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
} else {
// Default screen you set in info plist.
}