Facebook登录 - Firebase V3

时间:2016-05-21 12:35:33

标签: ios swift facebook firebase

我正在玩新版Firebase SDK,只是想添加Facebook登录。我能够通过Facebook登录用户,但这不会在实时数据库中创建记录。在以前版本的Firebase中,明确说明登录没有在数据库中创建用户引用,我们必须自己保存,在这个版本中它会自动创建记录,但这对我不起作用。这是我的代码:

import UIKit
import Firebase
import FBSDKCoreKit
import FBSDKLoginKit

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let loginButton = FBSDKLoginButton()
        loginButton.center = self.view.center
        self.view.addSubview(loginButton)
        loginButton.delegate = self
    }

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

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        if let error = error {
            print(error.localizedDescription)
            return
        } else {
            let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
            FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
                if let error = error {
                    print(error.localizedDescription)
                } else {
                    print("successfullyAuthenticated")
                }
            }
        }
    }

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

2 个答案:

答案 0 :(得分:4)

如果仔细查看代码

如果有人点击取消按钮或完成按钮,当facebook确认页面说出"你已经授权..yourappname"

时,你会发现它会崩溃

从Facebook获取该页面注销并尝试再次登录

希望它可以帮助你获得更好的代码......我也在研究新的火力基地......

像这样避免

        guard let accessToken:FBSDKAccessToken? = FBSDKAccessToken.currentAccessToken() else{

            return
        }

        if accessToken!.tokenString != nil {

            let credential = FIRFacebookAuthProvider.credentialWithAccessToken((accessToken?.tokenString)!)

            FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in

答案 1 :(得分:2)

不,它不会在您的Firebase数据库中创建记录,它会在您的Firebase项目中创建用户,并且它会显示在身份验证面板中。然后,您负责使用您可能需要的用户数据将其存储到数据库中。