连接到Facebook后无法连接到Firebase - 在Swift 3中

时间:2016-09-18 15:30:40

标签: ios firebase firebase-authentication swift3

在转移到Swift 3之前,用户必须通过Facebook登录该应用程序,然后它会自动登录到您的Firebase帐户。一切都很好,但后来我转移到Swift 3.登录按钮仍然存在,但在它要求登录Facebook的许可后 - 没有任何反应。

在转到Swift 3之前,这是我的代码:

var loginButton: FBSDKLoginButton = FBSDKLoginButton()

    override func viewDidLoad() {
        super.viewDidLoad()

        loginButton.hidden = true
        FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
            if let user = user {
                // User is signed in.
                currentEmail = user.email!
                print("DONE")
                NSNotificationCenter.defaultCenter().postNotificationName("dontShowLogInView", object: nil)
                } else {
                // No user is signed in.
                self.loginButton.center = self.view.center
                self.loginButton.readPermissions = ["public_profile", "email", "user_friends"]
                self.loginButton.delegate = self

                self.view.addSubview(self.loginButton)
                self.loginButton.hidden = false
            }
        }

        // Optional: Place the button in the center of your view.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        print("user logged in")
        self.loginButton.hidden = true
        activityIndicator.startAnimating()
        if error != nil {
            self.loginButton.hidden = false
            activityIndicator.stopAnimating()
            print("???")
            //handle errors
        } else if result.isCancelled {
            self.loginButton.hidden = false
            activityIndicator.stopAnimating()
            let loginManager = FBSDKLoginManager()
            loginManager.logOut()
            //handle cancel
            print("canceled")
        } else {
            let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
            FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
                print("user logged to firebase app")

            }
        }


    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
        print("user logged out")
    }

这是移动到Swift 3之后的代码:

var loginButton: FBSDKLoginButton = FBSDKLoginButton()

override func viewDidLoad() {
    super.viewDidLoad()
    //getFBUserData()
    loginButton.isHidden = true
    FIRAuth.auth()?.addStateDidChangeListener { auth, user in
        if let user = user {
            // User is signed in.
            currentEmail = user.email!
            print("DONE")
            print(currentEmail)
            NotificationCenter.default.post(name: Notification.Name(rawValue: "dontShowLogInView"), object: nil)
            } else {
            // No user is signed in.
            self.loginButton.center = self.view.center
            self.loginButton.readPermissions = ["public_profile", "email", "user_friends"]
            self.loginButton.delegate = self

            self.view.addSubview(self.loginButton)
            self.loginButton.isHidden = false
        }
    }

    // Optional: Place the button in the center of your view.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: NSError!) {
    print("user logged in")
    self.loginButton.isHidden = true
    activityIndicator.startAnimating()
    if error != nil {
        self.loginButton.isHidden = false
        activityIndicator.stopAnimating()
        print("???")
        //handle errors
    } else if result.isCancelled {
        self.loginButton.isHidden = false
        activityIndicator.stopAnimating()
        let loginManager = FBSDKLoginManager()
        loginManager.logOut()
        //handle cancel
        print("canceled")
    } else {
        let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
        FIRAuth.auth()?.signIn(with: credential) { (user, error) in
            print("user logged to firebase app")

        }
    }


}

func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
    print("user logged out")
}

0 个答案:

没有答案