在Swift中获取Facebook好友列表

时间:2014-10-23 23:05:58

标签: facebook facebook-graph-api swift

FBGraphUser对象没有属性friendsuser_friends

如果我打印FBGraphUser,这就是我得到的:

{
email = "tksybnk_goldmanstein_1414083483@tfbnw.net";
"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;
}

我要求的权限是:

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

当我在Graph API Explorer中执行请求时,我得到了正确的响应:

{
  "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
  }
}

有没有特殊的方式来访问朋友,还是应该在这本字典中打印?

3 个答案:

答案 0 :(得分:6)

这是我最终做的事情。与前2个权限不同,我不得不单独请求获取朋友:

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)")
        }
    })

答案 1 :(得分:2)

使用以下代码获取好友列表。

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")
}

答案 2 :(得分:0)

使用该应用获取朋友的ID,您可以在https://developers.facebook.com/apps创建测试用户和朋友进行测试

let _graphRequest = GraphRequest(graphPath: "me/friends", parameters: ["fields": "id"])
        _graphRequest.start(completionHandler: { (connection, result, error) in
            if error != nil {
                print("Error fb", error!.localizedDescription)
            }

            else {
                
                let field = result! as? [String: Any]
                var friendObjects = field!["data"] as! [NSDictionary]
                for friendObject in friendObjects {
                    print("found a friend:  \(friendObject["id"] as! NSString)")
                }
                
    
            }
        })