Jamie McAllister
Jamie McAllister

Reputation: 729

Read response from Facebook Graph Request in Swift 4

I'm trying to add a Facebook friend list to my Swift iOS app using the Graph API.

I'm struggling to work out how to actually access the data that's being send back from Facebook.

The response I get looks like this:

success(FacebookCore.GraphResponse(rawResponse: Optional({
"first_name" = Jamie;
id = 1626917907360895;
"last_name" = McAllister;
name = "Jamie McAllister";
picture =     {
    data =         {
        height = 50;
        "is_silhouette" = 0;
        url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/12994335_1101318013254223_4481895970110564364_n.jpg?oh=d10209f113213981e4417e7f6f3f82d8&oe=5A91F135";
        width = 50;
    };
};
})))

My graph Request is just /me for now as I'm the only registered user. But all the fields in the response are what I will be requesting from the friends list

The graph request looks like this:

var graph = GraphRequest.init(graphPath: "me")
    graph.parameters = ["fields": "id, first_name, last_name, middle_name, name, email, picture"]

    graph.start({ (response, data) in
        print("======GRAPH=====")
        print(data)

        })

So, I want to be able to take the GraphResponse, make it an Array and assign it to a variable. I am completely stumped by this one.

Upvotes: 1

Views: 3347

Answers (3)

Loup G
Loup G

Reputation: 189

The Graph request start completion block has changed to requiring 3 arguments instead of 2.

So you will have something like this (NOTICE THERE ARE NOW 3 ITEMS PASSED IN THE START BLOCK [connection, result, and error]):

let params = ["fields" : "email, first_name, id, picture"]
    let graphRequest = GraphRequest(graphPath: "me", parameters: params)
    graphRequest.start{
        (connection, result, error in

        if error == nil {
           if let responseDictionary = result as? NSDictionary {

            UserProfile.userName = responseDictionary["first_name"] as? String ?? "User"


           UserProfile.userID = responseDictionary["id"] as! String

           var pictureUrl = ""
           if let picture = responseDictionary["picture"] as? NSDictionary, let data = picture["data"] as? NSDictionary, let url = data["url"] as? String {
                                        pictureUrl = url

                                    }
                                }
        }else{
            print("error in graph request:", error)

        }

Upvotes: 2

Harjot Singh
Harjot Singh

Reputation: 6927

Hope this Helps Swift 4.2

    func getFbId(){
    if(AccessToken.current != nil){
        let req = GraphRequest(graphPath: "me", parameters: ["fields": "email,first_name,last_name,gender,picture"], accessToken: AccessToken.current, httpMethod: GraphRequestHTTPMethod(rawValue: "GET")!)
        req.start({ (connection, result) in
            switch result {
            case .failed(let error):
                print(error)

            case .success(let graphResponse):
                if let responseDictionary = graphResponse.dictionaryValue {
                    print(responseDictionary)
                    let firstNameFB = responseDictionary["first_name"] as? String
                    let lastNameFB = responseDictionary["last_name"] as? String
                    let socialIdFB = responseDictionary["id"] as? String
                    let genderFB = responseDictionary["gender"] as? String
                    let pictureUrlFB = responseDictionary["picture"] as? [String:Any]
                    let photoData = pictureUrlFB!["data"] as? [String:Any]
                    let photoUrl = photoData!["url"] as? String
                    print(firstNameFB, lastNameFB, socialIdFB, genderFB, photoUrl)
                }
            }
        })
    }
}

Upvotes: 3

Ganesh Manickam
Ganesh Manickam

Reputation: 2139

To read response from Facebook Graph Request use the below line of codes

let info = data as! [String : AnyObject]
 if info["name"] as? String != nil {
   self.usernameLbl.text = info["name"] as! String
 }

Hope this will help you

Upvotes: 1

Related Questions