Search code examples
iosswiftfirebasefirebase-authenticationfirebaseui

Trying to connect Facebook login via FirebaseUI Auth but having an issue with authUI.delegate = self


I am attempting to connect Facebook login via FirebaseUI Auth however I cannot get the Facebook connect button to appear in the instanced authViewController. Platform: iOS, Language: Swift 2.0, Latest stable Xcode release

Everything is working as it should as far as pulling up an instanced authViewController. The e-mail/password option is present and actively creates a new user in my Firebase database.

The problem I am having occurs when trying to implement Facebook login functionality;

In my AppDelegate.swift file I have the following:

import Firebase
import FirebaseAuthUI
...
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    FIRApp.configure()
    let authUI = FIRAuthUI.authUI()!
    authUI.delegate = self

    return true
}

The error I'm getting is "Cannot assign value of type 'AppDelegate' to type 'FIRAuthUIDelegate'" - basically I can't get the following line to work; authUI.delegate = self

I have read all documentation and browsed open/closed issues on implementation at the project's gitHub repo here: https://github.com/firebase/FirebaseUI-iOS/tree/master/FirebaseUI

I have looked over a previous StackOverflow question that is similar in nature here; How to use FirebaseUI for Google authentication on iOS in Swift?

I have attempted to copy the code from the above Stack Overflow since he states he did get more options to appear using his code (his question is about an error further down the line regarding authentication) but I still run into the same self delegation error. I have tried assigning FIRAuthUIDelegate to AppDelegate but it does not conform. What am I missing?


Solution

  • I'm also not familiar with FirebaseUI but here is a working example of authorizing a user with Facebook using regular Firebase and the FBSDKs

    @IBAction func fbButtonTapped(sender: UIButton) {
        let facebookReadPermissions = ["email", "public_profile", "user_photos"]
        FBSDKLoginManager().logInWithReadPermissions(facebookReadPermissions, fromViewController: self, handler: { (result:FBSDKLoginManagerLoginResult?, error:NSError?) -> Void in
            if error != nil {
                Helper.displayAlert("Error Logging into Facebook", message: error!.localizedDescription, viewController: self)
            } else {
                let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
                FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
                    if error != nil {
                        Helper.displayAlert("Error Logging into Facebook", message: error!.localizedDescription, viewController: self)
                    } else {
                    let request = FBSDKGraphRequest(graphPath:"me", parameters: ["fields": "id, first_name, last_name, email, age_range, gender, verified, timezone, picture"])
                        request.startWithCompletionHandler {
                            (connection, result, error) in
                            if error != nil {
                                print (error)
                            } else if let userData = result as? [String : AnyObject] {
                                guard let userID = FIRAuth.auth()?.currentUser?.uid else { return }
                                let userInfo = ["firstName": userData["first_name"] as! String, "lastName": userData["last_name"] as! String, "email": userData["email"] as! String, 
    "gender": userData["gender"] as! String, "id": userData["id"] as! String, "verified": userData["verified"]?.description as! AnyObject,  "key": userID]
    
    
                                FirebaseData.fbData.createFirebaseUser(userID, user: userInfo)
                                self.performSegueWithIdentifier(self.loginSucessIdentifier, sender: nil)
    
                            }
                            }
                        }
                    }
                }
            })
        }
    
    func createFirebaseUser(uid: String, user: [String : AnyObject]) {
        REF_USERS.child(uid).setValue(user)
    }
    

    The code could be cleaned up a bit to get rid of all the if let statements but it should get you going with a working authorization for facebook login.