2

I am working on an iOS 5 app where i need to get user profile data after successful login.

For now I am getting a access token when user get successful login.

Now I am trying to get Facebook logged in user profile data using that access token.

I tried to follow the sample app provided on the Facebook developer site.

I am not using FBconnect type resource file. I add "facebookSDK.framework" from sample app.

Please help me.

Thanks,

- (void)populateUserDetails {

FBRequestConnection *connection = [[FBRequestConnection alloc] init];

// First request uploads the photo.
FBRequest *request1 = [FBRequest requestForMe];


if (FBSession.activeSession.isOpen) {

    [connection addRequest:request1
         completionHandler:
     ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
         if (!error) {

             NSLog(@"fb user name = %@",user.name);
             NSLog(@"fb user name = %@",user.id);
         }

         NSLog(@"fb2 user name = %@",user.name);
         NSLog(@"fb2 user name = %@",user.id);
     }
            //batchEntryName:@"photopost"
     ];

    [connection start];
}

}

iOS Test
  • 1,103
  • 1
  • 11
  • 18
  • when i replace the if condition as in above code with if(objappdel.session.isOpen) then code executes but gives error code 400. and log null values. session is property of type: @property (strong, nonatomic) FBSession *session; – iOS Test Sep 15 '12 at 11:48

3 Answers3

12

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.

iSpark
  • 952
  • 7
  • 18
  • Thanks Murali..Any idea how can I get the profile_image url of the user – iOS Test Sep 15 '12 at 13:29
  • 1
    You can get ur profile picture by passing user id to FBProfilePictureView class.for that we have to take an outlet as IBOutlet FBProfilePictureView *profilePic to UIImageView.Now in above method,loginViewFetchedUserInfo:,pass user id like self.profilePic.profileID = user.id;now you can see your profile photo. – iSpark Sep 18 '12 at 06:34
4

This is the method to get user profile picture:

//in your app delegate file configure "FBProfilePictureView" class
- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    [FBProfilePictureView class];
}

//in your header file
IBOutlet FBProfilePictureView   *userPictureView;


@property (strong, nonatomic) FBProfilePictureView     *userPictureView;

//here "userPictureView" is uiview not uiimageview

//in your .m file 

-(void)setProfilePicture {
    [FBSession.activeSession closeAndClearTokenInformation];

    NSArray *permissions = [NSArray arrayWithObjects:@"email", nil];

    [FBSession openActiveSessionWithReadPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {
         NSLog(@"\nfb sdk error = %@", error);
         switch (state) {
             case FBSessionStateOpen:
                 [[FBRequest requestForMe] startWithCompletionHandler:
                  ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                      if (!error) {

                          self.userPictureView.profileID = [user objectForKey:@"id"];

                      } 
                  }];
                 break;
             case FBSessionStateClosed:
                 //need to handle
                 break;
             case FBSessionStateClosedLoginFailed:
                 //need to handle
                 break;
             default:
                 break;
         }
     }];
}
loganathan
  • 2,056
  • 2
  • 23
  • 34
1

I find it works best if you write the completion handler to treat the result as a FBGraphObject:

[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, FBGraphObject *result, NSError *error) {
  if (result && !error) {
    name = result[@"name"];
    email = result[@"email"];
    facebookId = result[@"id"];
  } else {
    NSLog(@"Error!");
  }
}];
Chris
  • 39,719
  • 45
  • 189
  • 235