从Facebook Graph api检索用户城市



城市是一个位置字段,但我似乎无法检索它。有人看到我做错了什么吗?

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

知道了。您必须使用位置{位置}。

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

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

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

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

最新更新