I’m using the graph api to return my name and email from a Facebook login. This is the code I use – name comes back fine, but email is nil:
func getUserInfo() {
let params = ["fields" : "email, name"]
let graphRequest = GraphRequest(graphPath: "me", parameters: params)
graphRequest.start {
(urlResponse, requestResult) in
switch requestResult {
case .failed(let error):
print("error in graph request:", error)
break
case .success(let graphResponse):
if let responseDictionary = graphResponse.dictionaryValue {
print(responseDictionary)
if let name = responseDictionary["name"] {
self.nameLabel.text = "Logged in as: n(name)"
}
if let email = responseDictionary["email"] {
self.emailLabel.text = "Your email is: n(email)"
} else {
self.emailLabel.text = "Your email is: "
}
print(responseDictionary["name"])
print(responseDictionary["email"])
}
}
}
}
This is what prints to my console:
["id": 1653899307960705, "name": *my name removed for privacy*]
Optional(*my name removed for privacy*)
nil
I’m not sure if my code is wrong or it’s just a bug – I specify both email and name as my parameters, but as you can see only the name comes back. Anyone know why this is happening?
2
Answers
Your code is absolutely correct, there few possibilities that facebook graph API not providing email:
Check account setting of an/your account (email is valid & validated as well as it is public)
I thing you forgot to take email permission, so here I am sharing you code to take permission.