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

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

问题描述:

下午好!我需要从Facebook获取用户数据.我成功获取了诸如用户名,ID,照片之类的数据.但是,当您尝试查询诸如婚姻状况之类的信息时,我得到一个空字典的响应.我尝试提出不同的要求,但总是得到这样的结果.我还阅读了官方Facebook网站,如我该如何解决?

Good afternoon! I need to get user data from Facebook. I successfully get data such as the user name, id, photo. 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?

我的示例代码

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

您的GraphRequest错误.如果要获取用户数据,graphPathe应该为"me",并且您应该请求参数以获取关系状态和其他信息.而且您还需要在LogInWithReadPermissons中请求公共配置文件,所以在读取权限中:-

Your GraphRequest was incorrect. 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. And also you need to request public profile in LogInWithReadPermissons, So In read permisson :-

    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 {

        }
    }

并且在检索信息时:-

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

顺便说一句,如果您想要婚姻状况,应该将graphPath用作我"而不是"/{user-id}/family".您可以将其用于获取此人的家庭关系.

By the way if you want marital status you should use graphPath as "me" not "/{user-id}/family". You can use that for get The person's family relationships.