2017-05-26 56 views
0

城市是一個位置的領域,但我似乎無法找回它。任何人看到我做錯了什麼?從Facebook的圖表檢索用戶城市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) 
     }) 

回答

1

明白了。您必須使用位置{位置}。

 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

您無法直接從Graph API獲取城市名稱,因此您只能獲取用戶的位置。從那個位置你也可以獲得城市名稱。

爲了得到位置,你需要設置權限和字段如下圖所示:

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

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

我解決了它。檢查我的答案,這是可能的。 –

相關問題