0

Swift3 iOS 10: I got an error when trying to get user information from facebook user. With the following snippet of code, I could access user data and it does works really well. As I change to login with another accounts it turns into error.

//Button
@IBAction func FBBtnPressed(_ sender: Any) {
    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) -> Void in
        if (error != nil){
            print(error) // Failed here...
        }else{
            let fbloginresult : FBSDKLoginManagerLoginResult = result!
            if(fbloginresult.grantedPermissions.contains("email"))
            {
                self.fetchProfile() //This function never calls as I change login account, it turns into error (Error 304)
            }
        }
    }
}
//Fetch user info...
func fetchProfile(){
    let parameters = ["fields": "name, email, gender, birthday, location, id, picture.type(large)"]

    FBSDKGraphRequest(graphPath: "me", parameters: parameters).start(completionHandler: {
        connection, result, error -> Void in
        if error != nil {
            print("longin error =\(String(describing: error))")
        } else {
            if let userInfo = result as? [String:Any]{   
                //Get user info...
                print(userInfo["name"])
                print(userInfo["email"])
                print(userInfo["gender"])
                print(userInfo["birthday"])
                print(userInfo["location"])
                print(userInfo["id"])

                }
            }
        }
    })
}
EK Chhuon
  • 1,105
  • 9
  • 15

1 Answers1

1

Sometimes fbloginresult.grantedPermissions will be nil.i am using below code

  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 ?? ""

                                print(firstName,lastName,email)

                                // here you can process your next step
                                self.fbLoginManager.logOut()
                            }
                        })
                    }
                } else {
                    print("facebook ---> login canceled")
                }
            } else {
                print(error?.localizedDescription ?? "facebook login has error")
            }
        }
Dharma
  • 3,007
  • 3
  • 23
  • 38