Reputation: 1914
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.
- (IBAction)login:(id)sender {
[FBSession openActiveSessionWithReadPermissions:@[@"email",@"user_location",@"user_birthday",@"user_hometown"]
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
switch (state) {
case FBSessionStateOpen:
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (error) {
NSLog(@"error:%@",error);
} else {
// retrive user's details at here as shown below
NSLog(@"FB user first name:%@",user.first_name);
NSLog(@"FB user last name:%@",user.last_name);
NSLog(@"FB user birthday:%@",user.birthday);
NSLog(@"FB user location:%@",user.location);
NSLog(@"FB user username:%@",user.username);
NSLog(@"FB user gender:%@",[user objectForKey:@"gender"]);
NSLog(@"email id:%@",[user objectForKey:@"email"]);
NSLog(@"location:%@", [NSString stringWithFormat:@"Location: %@\n\n",
user.location[@"name"]]);
}
}];
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
} ];
}
I used this code for get information, but I cannot get the any information. Can you help me about it? or Can you prefer a tutorial to read it? I have read tutorials on developer.facebook.com.
Thank you for your interest.
Upvotes: 34
Views: 50716
Reputation: 611
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];
}
}];
}
Upvotes: 38
Reputation: 982
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)")
}
})
Upvotes: 1
Reputation: 3761
Check out this lib: https://github.com/jonasman/JNSocialDownload
you can even get twitter
Upvotes: 0
Reputation: 914
Make the following Graph request:
/me?fields=name,picture.width(720).height(720){url}
And you get really large and cool profile picture:
{
"id": "459237440909381",
"name": "Victor Mishin",
"picture": {
"data": {
"url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/t31.0-1/c628.148.1164.1164/s720x720/882111_142093815957080_669659725_o.jpg"
}
}
}
P.S. /me?fields=picture.type(large)
doesn't do it for me.
Upvotes: 6
Reputation: 881
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";
Upvotes: 0
Reputation: 7959
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:
Upvotes: 80
Reputation: 2442
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.
Upvotes: 5