ios - Facebook iOS SDK 和 Swift : how to get user's hometown?

标签 ios swift fbsdk

我正在使用 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)")
            })

最佳答案

参见下面的示例,将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
                            }
                        }
                    }
                }
            }
        })
    }

另请参阅此链接 Fetching user details from Facebook in iOS

关于ios - Facebook iOS SDK 和 Swift : how to get user's hometown?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35699552/

相关文章:

ios - 需要显示所有项目,连续显示一个项目

ios - 因未捕获的异常而终止 - Swift 2 Parse

ios - 在 Xcode 6.1 中使用 Google API Objective-C 客户端导入 Google Drive SDK 时出错

ios - 我可以在 Facebook 中发布图像和文本而无需在 Objective c 中打开任何对话框或浏览器吗

ios - 如何将相机坐标转换为场景坐标空间?

ios - 使用 Spotify IOS SDK 获取每首轨道的封面艺术?

ios - viewWillAppear 在 cellForItemAtIndexPath 之后调用?

swift - 我可以在 watchOS 3 上将 UIGraphicsImageRenderer 与 WatchKit 一起使用吗?

Facebook 登录错误 - Xcode 8 GM

ios - Facebook 应用程序登录请求不在 FB iOS 应用程序中打开,它在 Safari 移动浏览器中打开