0

我正在使用 iOS 9.2 & Swift 2.1 & FBSDKVersion: 4.7.0。

我尝试使用 Graph API Explorer,当时我得到了所需的输出。

转换后的代码在 Objective-C 中,我将其更改为 Swift。

let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "hometown"], HTTPMethod: "GET")

            graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
                if ((error) != nil){
                    print("Error: \(error)")
                }
                else{
                    print("fetched details: \(result)")
            })
4

1 回答 1

0

请参见下面的示例并将hometown选项添加到FBSDKGraphRequest对象的“fields”参数中并更改:

func fetchUserProfile()
    {
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id, email, name, picture.width(480).height(480)"])

        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

            if ((error) != nil)
            {
                print("Error took place: \(error)")
            }
            else
            {
                print("Print entire fetched result: \(result)")

                let id : NSString = result.valueForKey("id") as! String
                print("User ID is: \(id)")

                if let userName = result.valueForKey("name") as? String
                {
                    self.userFullName.text = userName
                }

                if let profilePictureObj = result.valueForKey("picture") as? NSDictionary
                {
                    let data = profilePictureObj.valueForKey("data") as! NSDictionary
                    let pictureUrlString  = data.valueForKey("url") as! String
                    let pictureUrl = NSURL(string: pictureUrlString)

                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {

                        let imageData = NSData(contentsOfURL: pictureUrl!)

                        dispatch_async(dispatch_get_main_queue()) {
                            if let imageData = imageData
                            {
                                let userProfileImage = UIImage(data: imageData)
                                self.userProfileImage.image = userProfileImage
                                self.userProfileImage.contentMode = UIViewContentMode.ScaleAspectFit
                            }
                        }
                    }
                }
            }
        })
    }

另请参阅此链接 在 iOS 中从 Facebook 获取用户详细信息

于 2019-01-29T07:23:58.647 回答