1

I have followed the custom UI FBLogin button for my app.In LoginHomeViewController I have the FBLogin button.

Here is code which I am using in ViewDidLoad

FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];

if ([FBSDKAccessToken currentAccessToken])    
{        
    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil] startWithCompletionHandler: ^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {                
        if (!error)
        {
            NSLog(@"fetched user:%@", result);
        }
    }];
}

1) The issue is When first time there is no console output on safari or FB I am logging in I want user details in this view controller i.e. LoginHomeViewController.

2) Currently Once login done in safari or FB, when I goes back to previous view and enter into this controller i.e. LoginHomeViewController then am getting user details in

NSLog(@"fetched user:%@", result); 

I tried above code in Custom log in button click i.e.

- (IBAction)facebookClcik:(id)sender {
}

But am facing same issue.

Following are my AppDelegate methods

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions : (NSDictionary *)launchOptions 
{
    [FBSDKLoginButton class];
    return [[FBSDKApplicationDelegate sharedInstance] application : application didFinishLaunchingWithOptions : launchOptions];
}

- (BOOL)application : (UIApplication *)application openURL : (NSURL *)url sourceApplication : (NSString *)sourceApplication annotation : (id)annotation
{
    return [[FBSDKApplicationDelegate sharedInstance] application : application openURL : url sourceApplication : sourceApplication annotation : annotation];
}

- (void)applicationDidBecomeActive : (UIApplication *)application
{
    [FBSDKAppEvents activateApp];
}
Islam
  • 3,654
  • 3
  • 30
  • 40
Shrikant K
  • 1,988
  • 2
  • 23
  • 34
  • 1
    Why do you have `[FBSDKLoginButton class]` method inside `appDelegate`'s `application: didFinishLaunchingWithOptions:` method? – aramusss Jul 08 '15 at 11:57
  • http://stackoverflow.com/questions/30274693/unable-to-get-the-access-token-through-fbsdkaccesstoken-currentaccesstoken-in/30275960#30275960 refer this link – Dharmesh Dhorajiya Jul 08 '15 at 12:33
  • @DharmeshDhorajiya your solution is working when user is already logged in (in my above code is also working). I need details when i am clicking button facebookClcik in same view controller i.e. LoginHomeViewControlle when we come back from safari. – Shrikant K Jul 08 '15 at 12:48
  • @AshishKakkad that is not problem, my question is I need details when i am clicking button facebookClcik in LoginHomeViewController. Currently i am getting details when i go to previous view and enter into LoginHomeViewController. I need details within same controller once i click button. – Shrikant K Jul 08 '15 at 13:17
  • @ShrikantKankatti that what I am saying. save the data of facebook that you retrive – Ashish Kakkad Jul 08 '15 at 13:19
  • @AshishKakkad Dude come on..! m saying m not getting details first time and i don't want go back previous controller i need details within same class because after log in i want to show details here only. – Shrikant K Jul 08 '15 at 13:24
  • @ShrikantKankatti Use Login Manager for custom button. http://stackoverflow.com/a/30352673/3202193 Check my answer for swift or obj-c. I am getting data in same class. – Ashish Kakkad Jul 08 '15 at 13:27
  • @AshishKakkad I am agree with u if u already log in in Browser or FB App then m getting details no question, but if u not logged in Browser or FB App then m not getting details in same controller, m getting only when i go back and come into this controller. Where m making the mistake in the code? – Shrikant K Jul 09 '15 at 06:22
  • @ShrikantKankatti Sorry, I could not help you more. – Ashish Kakkad Jul 09 '15 at 06:24
  • What's the complete code you have inside `startWithCompletionHandler:` and what's the code for populating the data? – Islam Jul 18 '15 at 05:42
  • @aramusss I guess to prevent this type of [error](http://stackoverflow.com/questions/30311246/unknown-class-fbsdkloginbutton-in-interface-builder-file). – Islam Jul 18 '15 at 05:47

2 Answers2

0

Hope this way helps you to retrieve user details:

 - (IBAction)facebookLoginBtn:(id)sender {
appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
appDelegate.logOut=  true;


FBSession *session = [[FBSession alloc] initWithPermissions:@[@"publish_actions",@"email"]];
[FBSession setActiveSession:session];

[session openWithBehavior:FBSessionLoginBehaviorForcingWebView
        completionHandler:^(FBSession *session,
                            FBSessionState status,
                            NSError *error) {
            if (FBSession.activeSession.isOpen) {
                [[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.first_name);
                         appDelegate.UserName=[NSMutableString stringWithFormat:@"%@", user.first_name];
                         appDelegate.firstname =[NSMutableString stringWithFormat:@"%@", user.first_name];
                         appDelegate.idStr= user.id;
                         UserEmailStr= [user objectForKey:@"email"];
                        // [self fetchGuestuser];
                     }
                 }];
            }
        }];


  }

If you navigate user to safari, Apple may reject your app

for more refer this

Community
  • 1
  • 1
soumya
  • 3,801
  • 9
  • 35
  • 69
  • No FBSession in Facebook iOS SDK 4.0 -- http://stackoverflow.com/questions/29282769/no-fbsession-in-facebook-ios-sdk-4-0 – Shrikant K Jul 08 '15 at 12:06
  • oh sorry..I have not checked the latest SDK "FBSession and FBAccessTokenData are replaced by FBSDKLoginManager, FBSDKAccessToken. " . – soumya Jul 08 '15 at 12:23
0

try this code

if ([FBSDKAccessToken currentAccessToken])
    {
        NSLog(@"Token is available");

        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id,name,link,first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {
                 FbId = [NSString stringWithFormat:@"%@",[result objectForKey:@"id"]];
                 firstName = [NSString stringWithFormat:@"%@",[result objectForKey:@"first_name"]];
                 lastName = [NSString stringWithFormat:@"%@",[result objectForKey:@"last_name"]];
                 UserName = [NSString stringWithFormat:@"%@",[result objectForKey:@"name"]];

                 email_id = [NSString stringWithFormat:@"%@",[result objectForKey:@"email"]];
                 picture = [NSString stringWithFormat:@"%@",[[[result objectForKey:@"picture"] objectForKey:@"data"] objectForKey:@"url"]];
                 Password = [NSString stringWithFormat:@"%@",[result objectForKey:@"email"]];
//            user_id=[NSString stringWithFormat:@"%@",[result objectForKey:@"id"]];

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

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

                 [[NSUserDefaults standardUserDefaults]setObject:result forKey:@"FBData"];
                 [[NSUserDefaults standardUserDefaults]setObject:@"Yes" forKey:@"Check"];

                 [self fbLoginParseData];

             }
             else
             {
                 NSLog(@"Error %@",error);
             }
         }];
    }
    else
    {
        NSLog(@"User is not Logged in");
    }
Nikunj
  • 655
  • 3
  • 13
  • 25
  • 1) your solution is also same problem which i am facing, Once login done in safari or FB, when I goes back to previous view and enter into this controller i.e**.LoginHomeViewController** then am getting user details. I need once i click - (IBAction)facebookClcik:(id)sender { } – Shrikant K Jul 08 '15 at 12:25