Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get user data from Facebook SDK on iOS

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 =     (
    );
}
like image 458
Alexander Khitev Avatar asked Jun 02 '16 10:06

Alexander Khitev


People also ask

How do I find Facebook user data?

You can get user data by using the Facebook Graph API if the user has given their permission to share their data and your app has the appropriate permissions to receive this data. This topic shows you how to make a single request for data and how to have a batch of requests in a single request.

What is Facebook SDK for iOS?

The Facebook SDK is a set of software components that developers can include in their mobile app to understand how people use the app, run optimized marketing campaigns and enable Facebook login and social sharing. This course helps you understand the purpose of the Facebook SDK and App Events for Android and iOS.

What data does Facebook SDK collect?

The Facebook SDK collects the following information when you use Facebook Login: App Events: This covers generic App Events (e.g. App Install, app launch) and other standard logging for product metrics (e.g. SDK loading and SDK performance). Note that some of these events (e.g. App Installs, app launches etc.)


2 Answers

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.

like image 61
Chathuranga Silva Avatar answered Sep 24 '22 04:09

Chathuranga Silva


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")
        }
    })
like image 26
Raj Joshi Avatar answered Sep 24 '22 04:09

Raj Joshi