2016-10-10 20 views
2

我目前正在實施的Facebook登錄我的應用程序,我試圖獲取從配置文件的以下信息:斯威夫特FBSDK獲取用戶的國家

名字, 姓氏,性別 , 和國家。

但是,我似乎只能獲取除用戶國家以外的所有內容。我試圖把我的參數,我FBSDKGraphRequest這樣的:

let parameters = ["fields": "email, gender, user_location, id, first_name, last_name, picture.type(large)"] 

以及我readPermissions到:

loginButton.readPermissions = ["public_profile", "email", "user_location", "user_friends", "user_education_history", "user_birthday"] 

什麼我錯在這裏做什麼?我如何獲得用戶的國家?

非常感謝幫助!

回答

3

您的readPermissions user_location是正確的,但國家/地區的參數名稱不是user_location
這是location

let parameters = ["fields": "id,location,name,first_name,last_name,picture.type(large),email,birthday,gender,bio,relationship_status"] 

確保您已在Facebook帳戶中添加一個地方並作出公共在您的登錄,您將完成。嘗試使用您創建Facebook應用的同一個Facebook帳戶。

出於測試目的,您可以通過警告消息(如後面所示的截圖所示)獲取您的位置。但在將您的應用程序提交到appstore之前,您需要通過Facebook的登錄審覈,因爲user_location不是預先批准的許可。

對於更多信息,請到圖形API瀏覽器

你可以去Graph API Explorer,並嘗試與您的應用程序。點擊獲取令牌獲取用戶訪問令牌生成令牌。點擊它將打開權限複選框。請檢查user_location

Graph Api Explorer Page

enter image description here

接下來只要按一下有以下提交獲取參數。確保位置參數在那裏,您可以看到如圖所示的JSON。
位置字典與名稱鍵包括您的國家和城市的名稱。

{ 
    "id": "338058996547981", 
    "name": "Rajan Maheshwari", 
    "birthday": "04/29/1990", 
    "location": { 
    "id": "106517799384578", 
    "name": "New Delhi, India" 
    } 
} 

enter image description here

注: - 此外,你必須確保你已經在你的Facebook個人資料中輸入的地方,做出公開。

enter image description here

當你將運行具有權限的應用程序,你會得到一個警告屏幕這樣的Facebook驗證。

enter image description here

它會要求一個登錄評論一些權限的需要審查這裏。基本權限被Facebook這不需要任何審查提供了免費的: -

enter image description here

但是出於測試目的,你可以得到的位置沒有任何審查。但在您將iOS應用程序提交到appstore之前,您需要通過Facebook的登錄審查,以避免該警告消息並獲取用戶的位置。

+0

您好,感謝答案拉詹。這很奇怪,它在Graph API Explorer中有效,但在我的應用程序中不起作用(儘管我將其更改爲「位置」。嗯,還有什麼我忘記了? – askaale

+0

@askaale在我的應用程序中工作,請確保您的位置是公開的,並嘗試使用您創建應用程序的相同帳戶登錄 –

+0

我仍然無法解決此問題,我注意到我的位置不公開,但我改變了這一點,但它並沒有幫助。 ,它會是一個簡單的方法來獲取用戶的國家名稱嗎?或者我必須通過簡單地用逗號分隔字符串來獲得此信息嗎? – askaale

0

如果單獨需要的國家,你可以隨時使用位置{位置}可

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