Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Auth UI is not presented in iOS(Swift)

I want to try out Firebase Auth UI and I was following the official documents. The documents (at least for iOS) is pretty confusing and some what outdated (incorrect import naming and such). Now I just wanted to show the Firebase AuthUI screen at the start of my test app. I tried below

import UIKit
import FirebaseAuthUI
import FirebaseFacebookAuthUI
import FirebaseTwitterAuthUI

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if let authUI = FUIAuth.defaultAuthUI() {
            authUI.delegate = self

            // Setup login provider ( Need to import these seperately )
            authUI.providers = [ FUIFacebookAuth(), FUITwitterAuth() ]

            let authViewController = authUI.authViewController()
            present(authViewController, animated: true, completion: {})
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

extension ViewController: FUIAuthDelegate {
    func authUI(_ authUI: FUIAuth, didSignInWith authDataResult: AuthDataResult?, error: Error?) {
        // (Keep empty for now)
    }
}

authViewController is not presented somehow and I have no idea why. I tried put both the auth stuffs in both viewDidLoad and viewWillAppear but nothing work.

I also tried google around and look at the other tutorials and such but nothing solve my problem.

It would be great if anyone can point out my mistake or at least point me in some correct direction. Thanks

Edit1: I tried putting in the completion handler in the present and it did not get run, but authViewController isn't nil

like image 590
moka2258 Avatar asked Dec 04 '25 23:12

moka2258


1 Answers

You are attempting to present a new controller/screen in your viewWillAppear, meaning your screen is not fully loaded yet. So there should be a warning from Xcode that looks like so:

Warning: Attempt to present on whose view is not in the window hierarchy!

Now, move your code inside the viewDidAppear instead, like so:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if let authUI = FUIAuth.defaultAuthUI() {
        authUI.delegate = self

        // Setup login provider ( Need to import these seperately )
        authUI.providers = [ FUIFacebookAuth(), FUITwitterAuth() ]

        let authViewController = authUI.authViewController()
        present(authViewController, animated: true, completion: {})
    }
}

This should work. :)

like image 184
Glenn Posadas Avatar answered Dec 06 '25 12:12

Glenn Posadas