0

I am working with iOS 9.2 & Swift 2.1 & FBSDKVersion: 4.7.0.

I tried with Graph API Explorer, at that time I am getting the desired output.

The converted code is in Objective-C and I changed it to 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)")
            })
Bhagyalaxmi Poojary
  • 1,213
  • 1
  • 12
  • 17
Sudhi 9135
  • 745
  • 8
  • 17
  • What exactly isn't working? Are you getting an error, or just not the results you expected? – Lars Kristensen Feb 29 '16 at 12:31
  • As I understand, everything works, you have no problems, no questions... You just wanted to share what you did? – kostek Feb 29 '16 at 12:31
  • It's not working i want to get the hometown of the user who logged via facebook. Now i am getting id only. – Sudhi 9135 Feb 29 '16 at 12:36
  • check http://stackoverflow.com/questions/33124662/ios-get-users-hometown-with-swift-and-latest-fbsdk and http://stackoverflow.com/questions/32031677/facebook-graph-api-get-request-should-contain-fields-parameter-swift-faceb#32031890 – jose920405 Feb 29 '16 at 13:15
  • @jose920405 i find out the problem, i don't added user_location permission. I submitted user_hometown item for review. Now it's in review state. Do u know how long it will take. – Sudhi 9135 Mar 01 '16 at 05:32
  • @Sudhi9135 It can be up to 5 days if I remember correctly. Although in most cases it is less. – jose920405 Mar 01 '16 at 12:50

1 Answers1

0

See below example and add hometown option into "fields" parameter of FBSDKGraphRequest object and also change:

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

Also refer to this link Fetching user details from Facebook in iOS

Ashvini
  • 342
  • 3
  • 11
  • I know my answer is too late but it will be useful to them who will meet to this question in future. – Ashvini Jan 29 '19 at 07:25