Facebook登录无法在IOS SDK中使用

时间:2017-06-08 11:21:00

标签: ios swift3 facebook-sdk-4.x

我正在努力解决Facebook登录的问题,昨天Facebook登录工作正常但是今天当我运行我的应用程序时它没有工作不知道为什么,为什么它不能突然工作我已配置与Facebook登录相关的所有内容在Facebook开发控制台中,一切都已配置完毕 如果您有任何想法,请帮我解决,我已经启用了Keychain共享。enter image description here

Facebook登录代码

@IBAction func onClickFacebookLoginAction(_ sender: Any) {

    var message = String()
    if Reachability.isConnectedToNetwork() == true{

        let loginView:FBSDKLoginManager = FBSDKLoginManager()
        loginView.loginBehavior = FBSDKLoginBehavior.web
        loginView.logIn(withReadPermissions: ["email","public_profile","user_friends"], from: self, handler: { (result, error) in
            if(error != nil){

                print("Error while try to login with facebook-\(error?.localizedDescription)")
            }else if (result?.isCancelled)!{

                print("User cancel the facebook login")
            }else{

                if result?.grantedPermissions != nil{
                    if (result?.grantedPermissions .contains("email"))!{
                        self.ShowProgressHUD()
                        self.fetchUserInfo()
                    }else{
                        message = message.appending("Facebook email permission error")
                        self.showAlertMessage(message: message, title: "")
                    }
                }
            }
        })
    }
}

func fetchUserInfo() -> Void {

    if (FBSDKAccessToken.current() != nil) {

        let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "/me", parameters:["fields": "id, first_name, last_name, name, email, picture"])
        graphRequest.start(completionHandler: { (connection, result, error) in

            if(error != nil){

                self.showAlertMessage(message: (error?.localizedDescription)!, title: "")

            }
            else
            {
                 print("Result is:\(result)")
                self.dictionary = result as! [String : AnyObject]
                let name = self.dictionary["name"] as!String
                let email = self.dictionary["email"] as! String
                let token = FBSDKAccessToken.current().tokenString

                print("name is -\(name)")
                print("email is -\(email)")
                print("token is -\(token)")


                DispatchQueue.main.async {

                    let SelectionViewObj = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
                    self.navigationController?.pushViewController(SelectionViewObj, animated: true)
                }
            }
        })
    }
}

2 个答案:

答案 0 :(得分:0)

这就是我以前用Facebook登录的方式,

@IBAction func act_loginFB(_ sender: UIButton) {
    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.logIn(withReadPermissions: ["public_profile", "email",], from: self) { (result, error) -> Void in
        if (error == nil){
            let fbloginresult : FBSDKLoginManagerLoginResult = result!
            if(fbloginresult.grantedPermissions.contains("email"))
            {
                self.getFBUserData()
            }
        }
    }
}

func getFBUserData(){
    if((FBSDKAccessToken.current()) != nil){
        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
            if (error == nil){
                //everything works print the user data
                print(result)
                let data: [String:AnyObject] = (result as? [String : AnyObject])!
                print(data)
                let email: String = data["email"]! as! String
                print(email)
            }
        })
    }
}

检查您在info.plist中添加了正确的 FacebookAppID FacebookDisplayName

答案 1 :(得分:0)

这是我用来验证facebook.its的代码,就像魅力一样。只需检查或替换它。

self.fbLoginManager = FBSDKLoginManager.init()
fbLoginManager.loginBehavior = FBSDKLoginBehavior.web
self.fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) -> Void in
    if (error == nil) {
        let fbloginresult : FBSDKLoginManagerLoginResult = result!
        if fbloginresult.grantedPermissions != nil && fbloginresult.grantedPermissions.contains("email") {
            if((FBSDKAccessToken.current()) != nil){
                FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id, first_name, last_name, email, gender, birthday, location"]).start(completionHandler: { (connection, result, error) -> Void in
                    if error != nil {
                        print(error?.localizedDescription ?? "error in facebook login...!")
                        return
                    }
                    if let dict = result as? NSDictionary {
                        print("facebook login result --->\n\(dict)")
                        guard let id = dict["id"] else {
                            print("Ooops... id = nil")
                            return
                        }
                        //  let firstName: String = dict["first_name"] as? String ?? ""
                       // let lastName : String = dict["last_name"] as? String ?? ""
                      //  let email : String = dict["email"] as? String ?? ""

                        self.fbLoginManager.logOut()
                    }
                })
            }
        } else {
            print("facebook ---> login canceled")
        }
    } else {
        print(error?.localizedDescription ?? "facebook login has error")
    }
}