1

I am trying to add a FBLoginView in my app like described here.

FBLoginView *loginView = [[FBLoginView alloc] init];
[self.view addSubview:loginView];

I actually intend to hide it by setting

loginView.hidden = YES;

Then i want to trigger a click on loginView when some other event happens, something similar to this post said

Programmatically fire button click event?

But the FBLoginView does not receive a button click message. Is there still away to do it? Thanks!

Community
  • 1
  • 1
marsant
  • 1,088
  • 13
  • 30

5 Answers5

4

When you look into FBLoginView sources you'll see that FBLoginView is a subclass of UIView with UIButton added as a subview. There's no defined public property to that button, so it's not accessible in a normal way.

The way to do it is to iterate over subviews as suggested here: https://stackoverflow.com/a/12281054/317928. But it's a bad practice, because view hierarchy may change in future.

Also see Facebook login tutorial, section Login with API calls. This section describes how to use simple button for authentication.

Community
  • 1
  • 1
vokilam
  • 10,153
  • 3
  • 45
  • 56
4

Here is the code snippet that works for me.

for (id obj in loginView.subviews) {
    if ([obj isKindOfClass:[UIButton class]]) {
        UIButton * loginButton =  obj;
        [loginButton sendActionsForControlEvents:UIControlEventTouchUpInside];
        break;
    }
}

It will trigger Facebook login programmatically and can also be used to trigger logout. There is however an ugly effect for logout. This pop will show

enter image description here

at the bottom of screen. I hope there is a way to hide it too.

marsant
  • 1,088
  • 13
  • 30
4

In my opinion, much more elegant way to trigger Facebook login programmatically, is by managing session for example:

-(void)loginLogout
{
    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];

    }
    else
    {
    // Initialize a session object
    FBSession *session = [[FBSession alloc] initWithPermissions:@[@"publish_actions", @"user_photos"]];

    // Set the active session
    [FBSession setActiveSession:session];
    // Open the session
    [session openWithBehavior:FBSessionLoginBehaviorForcingWebView
            completionHandler:^(FBSession *session,
                                FBSessionState status,
                                NSError *error) {

                if(error == nil)
                {
                    NSLog(@"login successful");
                    // Respond to session state changes,
                    // ex: updating the view
                }
                else
                {
                    NSLog(@"login unsuccessful");
                }

            }];
    }
}

Moreover, changing session state will by noticed by hidden LoginView, and its delegate methods like

loginViewShowingLoggedInUser:(FBLoginView *)loginView

will be triggered (without mentioned earlier ugly pop).

kedzia
  • 295
  • 2
  • 12
  • I succeeded in getting "login successful" message, but loginViewShowingLoggedInUser delegate method is not triggered. – eugene Sep 29 '14 at 06:04
0

FBLoginView has two subviews, you can check in with subview of the view is the button located and then you can active the Action using the next code:

[loginView.subviews[0] sendActionsForControlEvents:UIControlEventTouchUpInside];
Ms. Nobody
  • 1,219
  • 3
  • 14
  • 34
0

I can trigger facebook with the following code:

for (id subview in self.facebookLoginView.subviews) {
    if ([subview isKindOfClass:[UIButton class]]) {
        UIButton *loginButton =  subview;
        [loginButton sendActionsForControlEvents:UIControlEventTouchUpInside];
        break;
    }
}

and for google plus (if smbdy is interested):

[self.googleLoginView sendActionsForControlEvents:UIControlEventTouchUpInside];

BE AWARE: if the SDK changes you have to retest it and modify it maybe!

Erhard Dinhobl
  • 1,646
  • 18
  • 34