For Swift 5
First add the fields that you need
let params = ["fields": "first_name, last_name, email, picture"]
Create the graph request
let graphRequest = GraphRequest(graphPath: "me", parameters: params, tokenString: token.tokenString, version: nil, httpMethod: .get)
graphRequest.start { (connection, result, error) in }
You will get the result in json
{
"first_name": "",
"last_name": "",
"picture": {
"data": {
"height": 50,
"is_silhouette": false,
"url": "",
"width": 50
}
},
"id": ""
}
According to the json response, catch the result
if let error = error {
print("Facebook graph request error: \(error)")
} else {
print("Facebook graph request successful!")
guard let json = result as? NSDictionary else { return }
if let id = json["id"] as? String {
print("\(id)")
}
if let email = json["email"] as? String {
print("\(email)")
}
if let firstName = json["first_name"] as? String {
print("\(firstName)")
}
if let lastName = json["last_name"] as? String {
print("\(lastName)")
}
if let profilePicObj = json["picture"] as? [String:Any] {
if let profilePicData = profilePicObj["data"] as? [String:Any] {
print("\(profilePicData)")
if let profilePic = profilePicData["url"] as? String {
print("\(profilePic)")
}
}
}
}
}
You can also get custom width profile image by sending the required width in the params
let params = ["fields": "first_name, last_name, email, picture.width(480)"]
This is how the whole code would like
if let token = AccessToken.current {
let params = ["fields": "first_name, last_name, email, picture.width(480)"]
let graphRequest = GraphRequest(graphPath: "me", parameters: params,
tokenString: token.tokenString, version: nil, httpMethod: .get)
graphRequest.start { (connection, result, error) in
if let error = error {
print("Facebook graph request error: \(error)")
} else {
print("Facebook graph request successful!")
guard let json = result as? NSDictionary else { return }
if let id = json["id"] as? String {
print("\(id)")
}
if let email = json["email"] as? String {
print("\(email)")
}
if let firstName = json["first_name"] as? String {
print("\(firstName)")
}
if let lastName = json["last_name"] as? String {
print("\(lastName)")
}
if let profilePicObj = json["picture"] as? [String:Any] {
if let profilePicData = profilePicObj["data"] as? [String:Any] {
print("\(profilePicData)")
if let profilePic = profilePicData["url"] as? String {
print("\(profilePic)")
}
}
}
}
}
}
Check out Graph API Explorer for more fields.