i have integrated Facebook sdk in Xcode 6 (with swift). During the login i request the public_profile permission:
FBSession.openActiveSessionWithReadPermissions(
With Facebook SDK 4.0, you can use:
Swift:
let pictureRequest = FBSDKGraphRequest(graphPath: "me/picture?type=large&redirect=false", parameters: nil)
pictureRequest.startWithCompletionHandler({
(connection, result, error: NSError!) -> Void in
if error == nil {
println("\(result)")
} else {
println("\(error)")
}
})
Objective-C:
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:[NSString stringWithFormat:@"me/picture?type=large&redirect=false"]
parameters:nil
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if (!error){
NSLog(@"result: %@",result);}
else {
NSLog(@"result: %@",[error description]);
}}];
The profile picture is in fact public and you can simply by adding the user id to Facebook's designated profile picture url address, ex:
var userID = user["id"] as NSString
var facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large"
This particular url address should return the "large" version of the user's profile picture, but several more photo options are available in the docs.
If you want to get the picture in the same request as the rest of the users information you can do it all in one graph request. It's a little messy but it beats making another request.
A more Swift 3 approach
let request = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, picture.type(large)"])
let _ = request?.start(completionHandler: { (connection, result, error) in
guard let userInfo = result as? [String: Any] else { return } //handle the error
//The url is nested 3 layers deep into the result so it's pretty messy
if let imageURL = ((userInfo["picture"] as? [String: Any])?["data"] as? [String: Any])?["url"] as? String {
//Download image from imageURL
}
})
Swift 2
let request = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, picture.type(large)"])
request.startWithCompletionHandler({ (connection, result, error) in
let info = result as! NSDictionary
if let imageURL = info.valueForKey("picture")?.valueForKey("data")?.valueForKey("url") as? String {
//Download image from imageURL
}
})
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.
Swift 4 approach :-
private func fetchUserData() {
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id, email, name, picture.width(480).height(480)"])
graphRequest?.start(completionHandler: { (connection, result, error) in
if error != nil {
print("Error",error!.localizedDescription)
}
else{
print(result!)
let field = result! as? [String:Any]
self.userNameLabel.text = field!["name"] as? String
if let imageURL = ((field!["picture"] as? [String: Any])?["data"] as? [String: Any])?["url"] as? String {
print(imageURL)
let url = URL(string: imageURL)
let data = NSData(contentsOf: url!)
let image = UIImage(data: data! as Data)
self.profileImageView.image = image
}
}
})
}
you can use this code For Swift 3.0 to get the user information
func getFbId(){
if(FBSDKAccessToken.current() != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id,name , first_name, last_name , email,picture.type(large)"]).start(completionHandler: { (connection, result, error) in
guard let Info = result as? [String: Any] else { return }
if let imageURL = ((Info["picture"] as? [String: Any])?["data"] as? [String: Any])?["url"] as? String {
//Download image from imageURL
}
if(error == nil){
print("result")
}
})
}
}