Getting User's Personal Info from Facebook in iOS

Getting user's Personal info from Facebook in iOS

To get user Email ID you must ask permission for email while logging.

FBSDKLoginButton *loginView = [[FBSDKLoginButton alloc] init];
loginView.readPermissions = @[@"email"];
loginView.frame = CGRectMake(100, 150, 100, 40);
[self.view addSubview:loginView];

You can get user email Id in New SDK using GraphPath.

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {

if (!error) {
NSLog(@"fetched user:%@ and Email : %@", result,result[@"email"]);
}
}];
}

result would get you all the user Details and result[@"email"] would get you the email for logged in user.

To get Profile picture you can use

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=normal",result[@"id"]]];
NSData *data = [NSData dataWithContentsOfURL:url];
_imageView.image = [UIImage imageWithData:data];

or u can also use FBSDKProfilePictureView to get profile Picture by passing user profile Id:

FBSDKProfilePictureView *profilePictureview = [[FBSDKProfilePictureView alloc]initWithFrame:_imageView.frame];
[profilePictureview setProfileID:result[@"id"]];
[self.view addSubview:profilePictureview];

Refer to :https://developers.facebook.com/docs/facebook-login/ios/v2.3#profile_picture_view

or u can also get both by passing as parameters

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
parameters:@{@"fields": @"picture, email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *pictureURL = [NSString stringWithFormat:@"%@",[result objectForKey:@"picture"]];

NSLog(@"email is %@", [result objectForKey:@"email"]);

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:pictureURL]];
_imageView.image = [UIImage imageWithData:data];

}
else{
NSLog(@"%@", [error localizedDescription]);
}
}];

Get Facebook user details with swift and parse

To get the user details you have to send a FBSDKGraphRequest after the login request.

This can be done inside the if let user = user {...} block.

    // Create request for user's Facebook data
let request = FBSDKGraphRequest(graphPath:"me", parameters:nil)

// Send request to Facebook
request.startWithCompletionHandler {

(connection, result, error) in

if error != nil {
// Some error checking here
}
else if let userData = result as? [String:AnyObject] {

// Access user data
let username = userData["name"] as? String

// ....
}
}

how to get user data from Facebook SDK on iOS

Your GraphRequest was incorrect. If you want to take user data, graphPathe should be "me" and you should request paramter in order to get relationship state and other information. And also you need to request public profile in LogInWithReadPermissons, So In read permisson :-

    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.loginBehavior = FBSDKLoginBehavior.Web
fbLoginManager.logInWithReadPermissions(["public_profile","email"], fromViewController: self) { (result, error) -> Void in
if error != nil {
print(error.localizedDescription)
self.dismissViewControllerAnimated(true, completion: nil)
} else if result.isCancelled {
print("Cancelled")
self.dismissViewControllerAnimated(true, completion: nil)
} else {

}
}

And When retrieving information :-

   FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, relationship_status"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
let fbDetails = result as! NSDictionary
print(fbDetails)
}
})

By the way if you want marital status you should use graphPath as "me" not "/{user-id}/family". You can use that for get The person's family relationships.

How to get user info from Facebook SDK in iOS?

After login authentication, you can get details from active FBSession like below

if (FBSession.activeSession.isOpen) {

[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
NSString *firstName = user.first_name;
NSString *lastName = user.last_name;
NSString *facebookId = user.id;
NSString *email = [user objectForKey:@"email"];
NSString *imageUrl = [[NSString alloc] initWithFormat: @"http://graph.facebook.com/%@/picture?type=large", facebookId];
}
}];
}

Update: Instead of id use objectID property, after release of version v3.14.1(May 12, 2014), the id property has been deprecated

NSString *facebookId = user.objectID;

get Facebook user profile data after getting access token in iOS 5

in your .h

#import<FacebookSDK/FacebookSDK.h>

Now in Your .m file just call below method to get user data

-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
NSLog(@"usr_id::%@",user.id);
NSLog(@"usr_first_name::%@",user.first_name);
NSLog(@"usr_middle_name::%@",user.middle_name);
NSLog(@"usr_last_nmae::%@",user.last_name);
NSLog(@"usr_Username::%@",user.username);
NSLog(@"usr_b_day::%@",user.birthday);

}

Above method is defined in FBLoginView.h you can see there.

This method i have added According to latest facebookSDK.framework.Hope it will help you.



Related Topics



Leave a reply



Submit