2

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)
        }
    }
}
123
  • 8,733
  • 14
  • 57
  • 99

3 Answers3

2

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.
}
123
  • 8,733
  • 14
  • 57
  • 99
vien vu
  • 4,277
  • 2
  • 17
  • 30
1

I don't know about in swift, but in objective-c it's as simple as this being thrown into viewDidLoad for your landing page:

PFUser *user = [PFUser currentUser];
if( user )
{
    //user's session token is still valid, navigate to appropriate page
}
else
{
    //display landing page to sign up / log in
}
Jake T.
  • 4,308
  • 2
  • 20
  • 48
  • Right, that's kind of the problem. If I put the user verification code I have above inside `viewDidLoad` of my landing page, the user will be `nil` in all cases. I'm not sure why this is happening, but I can only get `currentUser()` to not be `nil` if I put the user verification code inside `viewDidAppear`. I WANT it to be inside `viewDidLoad` but it doesn't work if I do that. – 123 Dec 09 '15 at 22:59
0

Try putting it in the init as follows:

required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //code
}

This is the first thing that should be called in the ViewController lifecycle

Alternatively, try having int in viewWillAppear:

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //code
}
Dimitre Bogdanov
  • 395
  • 1
  • 5
  • 18
  • Using `init` makes the user always return `nil`, and I've also tried `viewWillAppear` with unfortunately the same result. For some reason, it ONLY seems to work in `viewDidAppear`. – 123 Dec 09 '15 at 23:09