0

I instantiate Parse's stock UI Log-in screen via code on an empty VC. I want to then display an AlertController if User's email is not verified. So I'm presenting alertController over logInViewController that I've created by code.... I understand the error that AlertController is not in the window hierarchy, but I'm unsure how to solve it. It works if I dismiss logInViewController but I don't want to, I want it to exist in the background. Using Swift.

This answer did not get to the exact issue: AlertController is not in the window hierarchy

override func viewDidAppear(animated: Bool) {

        super.viewDidAppear(animated)
        if (PFUser.currentUser() == nil) {

            //build logInVC in Code:
            var logInViewController = PFLogInViewController()

            var logInLogoTitle = UILabel()

            logInLogoTitle.text = "Thredz"

            logInLogoTitle.font = UIFont(name: "Cochin", size: 40.0)

            logInViewController.fields = PFLogInFields.UsernameAndPassword | PFLogInFields.LogInButton | PFLogInFields.SignUpButton | PFLogInFields.PasswordForgotten | PFLogInFields.Twitter

            logInViewController.logInView?.backgroundColor = UIColor.whiteColor()

            logInViewController.logInView?.logo = logInLogoTitle

            logInViewController.delegate = self

            //present log in VC
            self.presentViewController(logInViewController, animated: true, completion: nil)

            //build signUpViewController
            var signUpViewController = PFSignUpViewController()

            signUpViewController.delegate = self

            var signUpLogoTitle = UILabel()

            signUpLogoTitle.text = "Thredz"

            signUpViewController.signUpView?.logo = signUpLogoTitle

            logInViewController.signUpController = signUpViewController

        }


    }



  func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser) {


        if (PFTwitterUtils.isLinkedWithUser(user)) {
            var twitterUsername = PFTwitterUtils.twitter()?.screenName

            PFUser.currentUser()?.username = twitterUsername

            PFUser.currentUser()?.saveEventually(nil)

        }

        if user["emailVerified"] as! Bool == true {
            dispatch_async(dispatch_get_main_queue()) {
                self.performSegueWithIdentifier(self.peopleTableViewControllerSegue, sender: nil)
            }
        } else {
            // User needs to verify email address before continuing
            let alertController = UIAlertController(
                title: "Email address verification",
                message: "We have sent you an email that contains a link - you must click this link before you can continue.",
                preferredStyle: UIAlertControllerStyle.Alert
            )
            alertController.addAction(UIAlertAction(title: "OK",
                style: UIAlertActionStyle.Default,
                handler: nil)
            )

            self.presentViewController(alertController, animated: true, completion: nil)
        }

    }
Community
  • 1
  • 1
GarySabo
  • 5,806
  • 5
  • 49
  • 124
  • Try presenting the `alertController` by calling `presentViewController` on the `logInController` – Steve Wilford Aug 18 '15 at 13:40
  • I see that you are trying to convert Parse's ObjC login tutorial project to Swift. The problem with this tutorial project is that it's a terrifyingly stupid way to set up a login system and doesn't need to be nearly as complex as they have it set up their code, futhremore, they are using artifacts like UIAlertView which is entirely different from a UIAlertController. I'd suggest using a different tutorial or method for logging in entirely. There's no reason you would ever need to present a view controller from another view controllers viewdidappear. I"ll try to find a better tut for you. – Larry Pickles Aug 18 '15 at 14:04
  • I'd appreciate that...yes this was a mashup of several tutorials. I originally had created my own custom log-in and sign-up, but scrapped and implemented the default because I feel more confident in Parse handling some of these things =) – GarySabo Aug 18 '15 at 14:24

1 Answers1

1

In your logInViewController(logInController:didLogInUser:) function, change the line

self.presentViewController(alertController, animated: true, completion: nil)

to

logInController.presentViewController(alertController, animated: true, completion: nil)

This will present the alert controller on top of the login controller instead of the empty view controller.

keithbhunter
  • 12,258
  • 4
  • 33
  • 58
  • Thank you! Makes so much sense now...I was presenting from self, but presenting from the previously presented VC preserves the hierarchy. – GarySabo Aug 18 '15 at 14:22