简体   繁体   English

获取用户的朋友用户ID以获取应用排行榜得分

[英]Get User's friends user ID for app leaderboard scores

So, i'm creating a leaderboard where users can see global scores, regional scores and friends scores who have used the app before. 所以,我正在创建一个排行榜,用户可以在其中查看之前使用过该应用的全球分数,区域分数和朋友分数。 all these scores are fetched and written to the fire store database in firebase along with the user's unique id when they log in with facebook. 所有这些分数都会被提取并写入firebase中的fire store数据库,以及用户使用facebook登录时的唯一ID。

I need to get a list of the users friends and their user ids from facebook so I can search the fire store database for their scores using the Facebook id. 我需要从Facebook获取用户朋友及其用户ID的列表,以便我可以使用Facebook ID在消防商店数据库中搜索他们的分数。 But I don't know how to get the id out of my result below when getting the list of friends: 但在获取朋友列表时,我不知道如何从我的结果中获取id:

func getFriendsFromFB() {

    let params = ["fields": "id, first_name, last_name, name, email, picture"]

    let graphRequest = FBSDKGraphRequest(graphPath: "/me/friends", parameters: params)
    let connection = FBSDKGraphRequestConnection()
    connection.add(graphRequest, completionHandler: { (connection, result, error) in
        if error == nil {
            if let userData = result as? [String:Any] {
                print(userData)
            }
        } else {
            print("Error Getting Friends \(error)");
        }

    })

    connection.start()

}

Can someone suggest how I get the user id out of my result in the code above? 有人可以建议我如何在上面的代码中从我的结果中获取用户ID?

Also I do ask for the user_friends permission when asking the user to login. 在要求用户登录时,我也要求user_friends权限。

You're casting the result as a Dictionary , this means the results are stored as key values. 您将结果转换为字典 ,这意味着结果存储为键值。 You can retrieve values by using dictionary["keyHere"] . 您可以使用dictionary["keyHere"]检索值。 I'm not sure how the FB graph result is structured, but you will have to do something like this: 我不确定FB图形结果是如何构造的,但你必须做这样的事情:

// Don't continue if the result is not a dictionary
guard let userData = result as? [String: Any] else {
    return
}

// Now, you can get values from the dictionary

// Let's say the userID is a String
if let userID = userData["id"] as? String {
    print(userID)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM