Facebook iOS SDK和Swift:如何获得用户的家乡?

时间:2016-02-29 12:27:03

标签: ios swift fbsdk

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

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

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

LinearLayout parent=new LinearLayout(this);
    View child=new View(this);
    float density =getResources().getDisplayMetrics().density;
    LinearLayout.LayoutParams lllp=new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    lllp.setMargins((int) (10*density), (int) (10*density), (int) (10*density), (int) (10*density));
    lllp.gravity=Gravity.CENTER;
    child.setPadding((int) (10*density), (int) (10*density), (int) (10*density), (int) (10*density));
    child.setLayoutParams(lllp);
    parent.addView(child);

1 个答案:

答案 0 :(得分:0)

请参见以下示例,并将家乡选项添加到 FBSDKGraphRequest 对象的“字段” 参数中,并进行以下更改:

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