how to get user data from Facebook SDK on iOS
Asked Answered
C

2

5

Good afternoon! I need to get user data from Facebook. I successfully get data such as the user name, id, photo. But when you try to query such as marital status, I get a response to an empty dictionary. I tried to make different requests, but always get such a result. I also read these themes official Facebook site, this, this like How can I fix this?

My example code

   static func getUserRelationship() {
    if (FBSDKAccessToken.currentAccessToken() != nil) {
        guard let currentUser = getCurrentFacebookUser() else { return }
        FBSDKGraphRequest(graphPath: "/\(currentUser.id)/family", parameters: nil, HTTPMethod: "GET").startWithCompletionHandler({ (requestConnection, result, error) in
            if error == nil {
                let dictionary = result as! [String : AnyObject]
                let array = dictionary["data"]
                print("facebook", result, dictionary, array?.count)
            } else {
                print(error.localizedDescription)
            }
        })
    } else {
        getDataLogin({
            getUserBirthday()
            }, fails: { (error) in

        })
    }
}

I see the result in the console

    facebook {
    data =     (
    );
}
Christiechristin answered 2/6, 2016 at 10:41 Comment(1)
Have you asked for the appropriate permissions? user_relationships developers.facebook.com/docs/facebook-login/…Fara
A
13

Your GraphRequest was incorrect. If you want to take user data, graphPathe should be "me" and you should request paramter in order to get relationship state and other information. And also you need to request public profile in LogInWithReadPermissons, So In read permisson :-

    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.loginBehavior = FBSDKLoginBehavior.Web
    fbLoginManager.logInWithReadPermissions(["public_profile","email"], fromViewController: self) { (result, error) -> Void in
        if error != nil {
            print(error.localizedDescription)
            self.dismissViewControllerAnimated(true, completion: nil)
        } else if result.isCancelled {
            print("Cancelled")
            self.dismissViewControllerAnimated(true, completion: nil)
        } else {

        }
    }

And When retrieving information :-

   FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, relationship_status"]).startWithCompletionHandler({ (connection, result, error) -> Void in
            if (error == nil){
                let fbDetails = result as! NSDictionary
                print(fbDetails)
            }
        })

By the way if you want marital status you should use graphPath as "me" not "/{user-id}/family". You can use that for get The person's family relationships.

Atherton answered 2/6, 2016 at 10:52 Comment(1)
Thank you so much. You helped me!Christiechristin
O
2

Get facebook user info update with swift 3

  FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, result, error) -> Void in
        if (error == nil){
            let fbDetails = result as! NSDictionary
            print(fbDetails)
        }else{
            print(error?.localizedDescription ?? "Not found")
        }
    })
Octangle answered 7/3, 2017 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.