从Facebook Graph api检索用户城市

时间:2017-05-26 02:56:41

标签: ios xcode facebook-graph-api swift3 nsdictionary

城市是一个位置的领域,但我似乎无法找回它。有人看到我做错了吗?

        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 个答案:

答案 0 :(得分:1)

知道了。您必须使用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
            }
        })

答案 1 :(得分:0)

您无法直接从图谱API获取城市名称,因此您只能获取用户的位置。从该位置,您也可以获得城市名称。

要获取您需要的位置,请设置权限和字段如下:

//Permission
["public_profile", "email", "user_location"]

//Fields for parameter
["fields":"email,location"]