用facebook帐户登录后如何执行segue?

时间:2017-07-27 16:32:52

标签: swift facebook firebase segue

目前我正在尝试在用户使用firebase登录Facebook后对第二个视图控制器执行segue

我能够解决这个问题。我的问题是我必须在激活Segue之前实际登录两次。有什么建议?

见下面的代码

private var fbLoginSuccess = false //This is gobal

override func viewDidAppear(_ animated: Bool) {
if (FBSDKAccessToken.current() != nil && fbLoginSuccess == true)
{

    performSegue(withIdentifier: "Home", sender: self)

}
 }

@IBAction func facebookLogin(sender: UIButton) {

let facebookLogin = FBSDKLoginManager()

facebookLogin.logIn(withReadPermissions: ["public_profile", "email"],        from: self, handler: {
    (facebookResult, facebookError) -> Void in
    if facebookError != nil {
        print("Facebook login failed. Error \(String(describing: facebookError))")
    } else if (facebookResult?.isCancelled)! {
        print("Facebook login was cancelled.")
    } else {


        let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)

        Auth.auth().signIn(with: credential) { (user, error) in
            if error != nil {
                print("Login failed. \(String(describing: error))")
            } else {
                 fbLoginSuccess = true
                print("Logged in!")

                if (facebookResult?.grantedPermissions.contains("email"))! {

                }
            }
            }
    }
        })
    }

1 个答案:

答案 0 :(得分:0)

您似乎只在performSegue(withIdentifier:)中呼叫viewDidAppear。如果您希望在登录后发生segue,则需要将其包含在那里。

let facebookLogin = FBSDKLoginManager()

facebookLogin.logIn(withReadPermissions: ["public_profile", "email"], from: self, handler: {
    (facebookResult, facebookError) -> Void in
    if facebookError != nil {
        print("Facebook login failed. Error \(String(describing: facebookError))")
    } else if (facebookResult?.isCancelled)! {
        print("Facebook login was cancelled.")
    } else {
        let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)

        Auth.auth().signIn(with: credential) { (user, error) in
            if error != nil {
                print("Login failed. \(String(describing: error))")
            } else {
                 fbLoginSuccess = true
                print("Logged in!")

                if (facebookResult?.grantedPermissions.contains("email"))! {

                }
                performSegue(withIdentifier: "Home", sender: self)
            }
        }
    }
})