Swift-从Facebook登录中获取错误 [英] Swift - Getting error from facebook login

查看:114
本文介绍了Swift-从Facebook登录中获取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我看了很多示例,但似乎找不到解决方案.好吧,我以为我做到了,但是似乎没有用.

just to start off with I have looked at many examples but can't seem to find a solution. Well I thought I did but it doesn't seem to work.

我想做的是为Facebook使用Firebase身份登录方法,该方法可以正常工作.我的问题是,当facebook方法引发电子邮件/凭据已存在的错误时,我想链接密码身份验证凭据和Facebook身份验证凭据.我在此处阅读了可以使用error.email的信息,但它没有给我访问email.error的选项.我可能会丢失一些东西,并且花了很多时间试图找出答案.

What I'm trying to do is use the Firebase auth login method for facebook, which works by the way. My issue is I want to link the password auth credentials and facebook auth credentials when the facebook method throws the error that the email/credentials already exist. I read here that I could use error.email but it doesn't give me the option to access the email.error. I might be missing something and I have spent a lot of time trying to figure it out.

这是我的代码:

func signInFacebookUser() {
    let fbLoginManager = FBSDKLoginManager()
    fbLoginManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (result, error) in
        if let error = error {
            self.errorMessagePopUp(title: "Failed to login", message: error)
            return
        }

        guard let accessToken = FBSDKAccessToken.current() else {
        print("Failed to get access token")
        return
   }

   let credential = FacebookAuthProvider.credential(withAccessToken: accessToken.tokenString)

   Auth.auth().signIn(with: credential, completion: { (user, error) in
        if let error = error {

        let errCode = AuthErrorCode(rawValue: error._code)!

        switch errCode {

              case .accountExistsWithDifferentCredential:
              // somehow obtain email right here.
              self.showSigninForm(attributes: self.attributes(), style: .light)
              return

             default:
             return
          }
       }

       self.performSegue(withIdentifier: "signInBtnPressed", sender: nil)
    })
  }
}

推荐答案

尝试此代码

@IBAction func facebookLoginButtonTapped(_ sender: UIButton) {
        if Utility.checkInternet(){
            let fbLoginManager: FBSDKLoginManager = FBSDKLoginManager()
            fbLoginManager.logIn(withReadPermissions: ["email", "public_profile"], from: self) { (result, error) in
                if (error == nil){
                    let fbLoginResult: FBSDKLoginManagerLoginResult = result!
                    if fbLoginResult.grantedPermissions != nil {
                        if(fbLoginResult.grantedPermissions.contains("email")){
                            self.facebookLogin()
                        }
                    }
                }
            }
        }
        else{
            Utility.showAlert("Connection Error!", message: "Please check internet connection and retry.", viewController: (self.window?.rootViewController)!)
        }
    }


func facebookLogin() {
        Utility.showProgress("")
        if((FBSDKAccessToken.current()) != nil) {

            FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id, name, first_name, last_name, picture.type(large), email "]).start(completionHandler: { (connection, result, error) in
                if result == nil {
                }
                else{
                    self.userResults = result as! [String : AnyObject]
                    self.email = (self.userResults["email"] as? String) ?? ""
                    if (self.email != "") {

                    }
                    else{
                        Utility.showAlert("Login Faild", message: error?.localizedDescription, viewController: self)
                    }
                }
            })
        }
    }

这篇关于Swift-从Facebook登录中获取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆