I have read a lot of tutorials about getting information from Facebook, but I have failed so far. I just want to get username and profile picture from Facebook.
Actually using "http://graph.facebook.com//picture?type=small" to fetch the profile image of the user or even their friends is slow.
A better and faster way of doing it to add a FBProfilePictureView object to your view and in it's profileID property, assign the user's Facebook id.
For example: FBProfilePictureView *friendsPic;
friendsPic.profileID = @"1379925668972042";
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields" : @"id,name,picture.width(100).height(100)"}]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *nameOfLoginUser = [result valueForKey:@"name"];
NSString *imageStringOfLoginUser = [[[result valueForKey:@"picture"] valueForKey:@"data"] valueForKey:@"url"];
NSURL *url = [[NSURL alloc] initWithURL: imageStringOfLoginUser];
[self.imageView setImageWithURL:url placeholderImage: nil];
}
}];
}
This is the simplest way I've found to get the user's profile picture.
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *FBuser, NSError *error) {
if (error) {
// Handle error
}
else {
NSString *userName = [FBuser name];
NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", [FBuser objectID]];
}
}];
Other query parameters that can be used are:
Check out this lib: https://github.com/jonasman/JNSocialDownload
you can even get twitter
This is the code for Facebook SDK 4 and Swift:
if FBSDKAccessToken.currentAccessToken() != nil {
FBSDKGraphRequest(graphPath: "me", parameters: nil).startWithCompletionHandler({ (connection, result, error) -> Void in
println("This logged in user: \(result)")
if error == nil{
if let dict = result as? Dictionary<String, AnyObject>{
println("This is dictionary of user infor getting from facebook:")
println(dict)
}
}
})
}
UPDATE TO ANSWER QUESTIONS:
To download public profile image, you get the facebook ID from the dictionary:
let facebookID:NSString = dict["id"] as AnyObject? as NSString
And then call a request to graph API for profile image using the facebook ID:
let pictureURL = "https://graph.facebook.com/\(fbUserId)/picture?type=large&return_ssl_resources=1"
Sample code:
let pictureURL = "https://graph.facebook.com/\(fbUserId)/picture?type=large&return_ssl_resources=1"
//
var URLRequest = NSURL(string: pictureURL)
var URLRequestNeeded = NSURLRequest(URL: URLRequest!)
println(pictureURL)
NSURLConnection.sendAsynchronousRequest(URLRequestNeeded, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!, error: NSError!) -> Void in
if error == nil {
//data is the data of profile image you need. Just create UIImage from it
}
else {
println("Error: \(error)")
}
})
You could also get the username and picture as follows:
[FBSession openActiveSessionWithReadPermissions:@[@"basic_info"]
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
if(!error && state == FBSessionStateOpen) {
{ [FBRequestConnection startWithGraphPath:@"me" parameters:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"id,name,first_name,last_name,username,email,picture",@"fields",nil] HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSDictionary *userData = (NSDictionary *)result;
NSLog(@"%@",[userData description]);
}];
}
}
}];
Output:
picture = {
data = {
"is_silhouette" = 0;
url = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-frc1/t5.0-1/xxxxxxxxx.jpg";
};
};
username = xxxxxxxxx;
You could just leave the parameter to picture & username and exclude the others based on you requirement. HTH.