简体   繁体   English

如何从iOS上的Facebook SDK获取用户数据

[英]how to get user data from Facebook SDK on iOS

Good afternoon! 下午好! I need to get user data from Facebook. 我需要从Facebook获取用户数据。 I successfully get data such as the user name, id, photo. 我成功获取了用户名,ID,照片等数据。 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? 我也看过这些主题官方Facebook网站这个这个怎么样才能解决这个问题?

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 =     (
    );
}

Your GraphRequest was incorrect. 您的GraphRequest不正确。 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. 如果你想获取用户数据,graphPathe应该是“我”,你应该请求参数以获得关系状态和其他信息。 And also you need to request public profile in LogInWithReadPermissons, So In read permisson :- 而且你还需要在LogInWithReadPermissons中请求公共资料,所以在阅读许可: -

    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". 顺便说一句,如果你想要婚姻状况你应该使用graphPath作为“我”而不是“/ {user-id} / family”。 You can use that for get The person's family relationships. 你可以用它来获得这个人的家庭关系。

Get facebook user info update with swift 3 用swift 3获取facebook用户信息更新

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

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

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