I am working on an ios app which requires Facebook Login.I have successfully implemented the login process.But now I am not able to find that how and where can i get the user's profile information like first name,last name, Profile Pic etc...My app has permissions to access firstName,lastName,Profile pic and Email.
Asked
Active
Viewed 5,337 times
3
-
https://developers.facebook.com/docs/ios/graph – Nike Kov Oct 04 '16 at 10:45
2 Answers
14
Below is the code for getting person public_profile using latest Facebook SDK v4.0.1. You need to use FBSDKProfile to get person profile.
-(void)viewDidLoad{
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
[self.view addSubview:loginButton];
self.loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
self.loginButton.delegate = self;
[FBSDKProfile enableUpdatesOnAccessTokenChange:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(profileUpdated:) name:FBSDKProfileDidChangeNotification object:nil];
}
-(void)profileUpdated:(NSNotification *) notification{
NSLog(@"User name: %@",[FBSDKProfile currentProfile].name);
NSLog(@"User ID: %@",[FBSDKProfile currentProfile].userID);
}
- (void) loginButton:(FBSDKLoginButton *)loginButton
didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
error:(NSError *)error{
}
- (void)loginButtonDidLogOut:(FBSDKLoginButton *)loginButton{
}

Sivajee Battina
- 4,124
- 2
- 22
- 45
-
This function is not getting called at all - (void) loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error – Junior Bill gates Apr 06 '15 at 10:40
-
-
yeah i already have included it but the function was not getting called. – Junior Bill gates Apr 06 '15 at 11:11
-
your new edit let me extract the user id and name Thanks .Can u plz tell me how can i extract email and profile picture – Junior Bill gates Apr 06 '15 at 11:13
-
The FBSDKProfilePictureView class provides an easy way to add someone's Facebook profile image to your view. Simply set the profileID property on the instance. You can set it to a value of me to automatically track the person currently logged-in. – Sivajee Battina Apr 08 '15 at 06:45
-
-2
Use the Notification method so the when it receives information from facebook it ca call facebook delegated method
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleFBSessionStateChangeWithNotification:)
name:@"SessionStateChangeNotification"
object:nil];
After that implement this method
-(void)handleFBSessionStateChangeWithNotification:(NSNotification *)notification{
NSDictionary *userInfo = [notification userInfo];
FBSessionState sessionState = [[userInfo objectForKey:@"state"] integerValue];
NSError *error = [userInfo objectForKey:@"error"];
// Usually, the only interesting states are the opened session, the closed session and the failed login.
if (!error) {
// In case that there's not any error, then check if the session opened or closed.
if (sessionState == FBSessionStateOpen) {
[FBRequestConnection startWithGraphPath:@"me"
parameters:@{@"fields": @"first_name, last_name, picture.type(normal), email"}
HTTPMethod:@"GET"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Set the use full name.
NSLog(@"first name and last name is %@",[NSString stringWithFormat:@"%@ %@",
[result objectForKey:@"first_name"],
[result objectForKey:@"last_name"]
]);
NSString *firstName = [NSString stringWithFormat:@"%@",[result objectForKey:@"first_name"]];
NSString *middleName = [NSString stringWithFormat:@"%@",[result objectForKey:@"middle_name"]];
NSString *lastName = [NSString stringWithFormat:@"%@",[result objectForKey:@"last_name"]];
NSString *email = [NSString stringWithFormat:@"%@",[result objectForKey:@"email"]];
// Set the e-mail address.
NSLog(@"email is %@", [result objectForKey:@"email"]);
}
else{
NSLog(@"%@", [error localizedDescription]);
}
}];
// [self.btnToggleLoginState setTitle:@"Logout" forState:UIControlStateNormal];
}
else if (sessionState == FBSessionStateClosed || sessionState == FBSessionStateClosedLoginFailed){
// A session was closed or the login was failed. Update the UI accordingly.
// [self.btnToggleLoginState setTitle:@"Login" forState:UIControlStateNormal];
// self.lblStatus.text = @"You are not logged in.";
}
}
}

Shruti
- 1,849
- 1
- 13
- 21