Search code examples
iosobjective-cfacebookfacebook-graph-apifacebook-ios-sdk

How to get user data when using custom button and FBSession for login


I am using a custom button implementation for FBLogin and not using FBLoginView Class. According to documentation I implemented FBSession methods in IBAction of button:

// If the session state is any of the two "open" states when the button is clicked
if (FBSession.activeSession.state == FBSessionStateOpen
    || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {

    // Close the session and remove the access token from the cache
    // The session state handler (in the app delegate) will be called automatically
    [FBSession.activeSession closeAndClearTokenInformation];

    // If the session state is not any of the two "open" states when the button is clicked
} else {
    // Open a session showing the user the login UI
    // You must ALWAYS ask for public_profile permissions when opening a session
    [FBSession openActiveSessionWithReadPermissions:@[@"public_profile"]
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session, FBSessionState state, NSError *error) {

         // Retrieve the app delegate
         AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
         // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
         [appDelegate sessionStateChanged:session state:state error:error];
     }];
}

Now as I get the user data in FBLoginView Delegate:

-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
NSLog(@"user info %@", user);}

The user gives me all my releavant data like user first and last names, email id etc. I want similiar object from Custom UI implementation. How can I achieve this?


Solution

  • [[FBRequest requestForMe] startWithCompletionHandler:
              ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                  if (!error) {
                      NSLog(@"accesstoken %@",[NSString stringWithFormat:@"%@",session.accessTokenData]);
                      NSLog(@"user id %@",user.id);
                      NSLog(@"Email %@",[user objectForKey:@"email"]);
                      NSLog(@"User Name %@",user.username);
                  }
              }];