Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Facebook friends list in Swift

FBGraphUser object doesn't have the property friends or user_friends.

If I print the FBGraphUser, this is what I get:

{
email = "[email protected]";
"first_name" = Maria;
gender = female;
id = 1515324765375152;
"last_name" = Goldmanstein;
link = "https://www.facebook.com/app_scoped_user_id/1515324765375152/";
locale = "en_US";
"middle_name" = Amfibibiijdh;
name = "Maria Amfibibiijdh Goldmanstein";
timezone = 0;
"updated_time" = "2014-10-23T16:58:11+0000";
verified = 0;
}

The permissions I request are:

self.fbLoginView.readPermissions = ["public_profile", "email", "user_friends"]

When I do the request in the Graph API Explorer, I do get the proper response:

{
  "data": [
    {
      "name": "Open Graph Test User", 
      "id": "289719481225025"
    }
  ], 
  "paging": {
    "next": "https://graph.facebook.com/v2.1/1515324765375152/friends?limit=5000&offset=5000&__after_id=enc_AezwfD79EK96eREIyB6ZR8fvDu4G4q_wRb49JG4FYp_kNnWK2DNcdLt_LMnDodVraWMBXVR89Cw0FcTYMPmYSt1Q"
  }, 
  "summary": {
    "total_count": 1
  }
}

Is there a special way to access the friends, or should they be printed in this dictionary?

like image 487
user83039 Avatar asked Oct 23 '14 23:10

user83039


2 Answers

Here is what I ended up doing. Unlike the first 2 permissions, I had to make a separate request to get the friends:

FBRequestConnection.startForMyFriendsWithCompletionHandler({ (connection, result, error: NSError!) -> Void in
        if error == nil {
            var friendObjects = result["data"] as [NSDictionary]
            for friendObject in friendObjects {
                println(friendObject["id"] as NSString)
            }
            println("\(friendObjects.count)")
        } else {
            println("Error requesting friends list form facebook")
            println("\(error)")
        }
    })
like image 79
user83039 Avatar answered Nov 17 '22 01:11

user83039


Get Friends list using below code.

var friendsRequest : FBRequest = FBRequest.requestForMyFriends()
friendsRequest.startWithCompletionHandler
{
    (connection:FBRequestConnection!,   result:AnyObject!, error:NSError!) -> Void in
    var resultdict = result as NSDictionary
    println("Result Dict: \(resultdict)")
    var data : NSArray = resultdict.objectForKey("data") as NSArray

    for i in 0 ..< data.count 
    {
      let valueDict : NSDictionary = data[i] as NSDictionary
      let id = valueDict.objectForKey("id") as String
      println("the id value is \(id)")
    }

    var friends = resultdict.objectForKey("data") as NSArray
    println("Found \(friends.count) friends")
}
like image 2
Vikram Pote Avatar answered Nov 17 '22 01:11

Vikram Pote