skip to Main Content

City is a field of location but I just can’t seem to retrieve it. Anyone see what I’m doing wrong?

        FBSDKGraphRequest(graphPath: "me/location", parameters: ["fields": "city"]).start(completionHandler: { (connection, result, error) -> Void in
            let location = result as! NSDictionary
            let city = location.value(forKey: "city") as! String
            print(city)
        })

2

Answers


  1. Chosen as BEST ANSWER

    Got it. You must use location{location}.

            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "location{location}"]).start(completionHandler: { (connection, result, error) -> Void in
                if let error = error {
                    self.alertTheUser(title: "Error", message: error.localizedDescription)
                    print(error)
                }else{
                    let fbDetails = result as! NSDictionary
                    let location : NSDictionary! = fbDetails.value(forKey: "location") as! NSDictionary
                    let locationContents : NSDictionary! = location.value(forKey: "location") as! NSDictionary
                    self.registerCityField.text = locationContents.value(forKey: "city") as? String
                    self.registerStateField.text = locationContents.value(forKey: "state") as? String
                    self.registerCountryField.text = locationContents.value(forKey: "country") as? String
                }
            })
    

  2. You can not get City Name directly from Graph API so you can only get the location of user. From that location you can get the city name as well.

    To get the location you need set permission and fields Like Below :

    //Permission
    ["public_profile", "email", "user_location"]
    
    //Fields for parameter
    ["fields":"email,location"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search