2016-09-19 114 views
0

我在轉到Swift 3後面臨一個問題 - 在用戶單擊連接到Facebook登錄按鈕後,沒有任何反應。登錄到Facebook後未登錄到Firebase Swift 3

這是移動到斯威夫特3後,我的代碼:

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) { 
     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") 

      } 
     } 

它不打印user logged in,也不能打印user logged to firebase app

回答

0

我希望這個代碼將幫助你。

func fbLogin() { 
    let facebookLogin = FBSDKLoginManager() 

    // MARK:- Sign In with facebook. 
    facebookLogin.logIn(withReadPermissions: ["email"], from: self) { (facebookResult, facebookError) -> Void in 
     if facebookError != nil { 
      print("Facebook login failed. Error \(facebookError.debugDescription)") 
      // handle if there is an error 
     } else if (facebookResult?.isCancelled)! { 
      print("Facebook login was cancelled") 
      // handle if facebook login was canceled 
     } else { 
      // Sign in to Firebase with facebook credential. 
      let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString) 
      FIRAuth.auth()?.signIn(with: credential) { user, error in 
       if error != nil { 
        print("Login failed. \(error.debugDescription)") 
        // handle alert for error 
       } else { 
        guard let currentUser = user else { return } 
        print("Login success \(currentUser.uid)") 
        // do something with currentUser 

       } 
      } 
     } 
    } 
}