知道在ios中按下Facebook登录按钮 [英] Know when facebook login button is pressed in ios

查看:135
本文介绍了知道在ios中按下Facebook登录按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某人按下Facebook登录按钮时显示HUD。目前我很难知道什么时候按下按钮。

I want to show a HUD when somebody press the facebook login button. Currently I struggle to know when the button was pressed.

我试图抓住所有的水龙头,但这并不适用于Facebook按钮上的任何共鸣...

I tried to grab all taps of the view, but this does not work for any reson on the facebook button...

如何知道什么时候Facebook登录被按下,呼叫我的HUD?

另外,功能facebook登录按钮触发器。无法找到..

Additional it would be nice to know which function the facebook login button triggers. Could not find out..

// workaround to find out when user tapped the login button
-(void)addTapFinder {
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];

    tapped.numberOfTapsRequired = 1;
    tapped.delegate = self;
    tapped.cancelsTouchesInView = NO;

    FBLoginView *fbButton = [[FBLoginView alloc] init];
    UIView * fbView = (UIView *)fbButton;
    fbView = [self.view viewWithTag:fbloginViewTAG];

    [fbView addGestureRecognizer:tapped];

    DLog(@"found View %@", fbView);
}


-(void)tapped {
    DLog(@"tapped");
}



调试器日志



Debugger log

[872:60b] -[LoginViewController FacebookLogin] [Line 283] loginview subviews: <FBShadowLabel: 0x16d17840; baseClass = UILabel; frame = (33 -1; 168 46); text = 'Connect  Facebook'; autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x18136240>>
[872:60b] -[LoginViewController showFBLogin] [Line 231] show login View
[872:60b] -[LoginViewController tapped] [Line 250] tapped   // tapped somewhere in self.view, but not the login button
// tapped the login button, no "tapped" entry in the logfile

下面完整的代码:

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#import "WebApi.h"
#import "SVProgressHUD.h"

@interface LoginViewController : UIViewController <FBLoginViewDelegate, UIGestureRecognizerDelegate>

-(IBAction)toSurroundView;

@property (nonatomic, strong) WebApi *swebapi;
@property (nonatomic) BOOL isFirstFBLoginDone;
@property (weak, nonatomic) IBOutlet UIView *loginView;

@end



LoginViewController.m



LoginViewController.m

#import "LoginViewController.h"

@interface LoginViewController ()
@end

@implementation LoginViewController


static int const fbloginViewTAG = 203;

-(void)vinit {
    self.swebapi = [[WebApi alloc] init];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"didRegisterUser" object:nil ];
    self.isFirstFBLoginDone = FALSE;
}

- (void)viewDidLoad
{
    [self vinit];
    [super viewDidLoad];
    [self showFBLogin];
}


-(void)handleNotification:(NSNotification *)message {
    if ([[message name] isEqualToString:@"didRegisterUser"]) {
        [self toSurroundView];
    }
}

-(IBAction)toSurroundView {
    [SVProgressHUD dismiss]; // connecting to Facebook, this is called to late!

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *surroundViewController = [mainStoryboard instantiateInitialViewController];
    surroundViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:surroundViewController animated:YES completion:nil];
}

#pragma mark - Facebook Things

-(void)showFBLogin {
    UIView *connectView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:connectView];
    [self FacebookLogin];   
    [self addTapFinder]; 
    DLog(@"show login View");
}

// workaround to find out when user tapped the login button
-(void)addTapFinder {
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];

    tapped.numberOfTapsRequired = 1;
    tapped.delegate = self;
    tapped.cancelsTouchesInView = NO;

    FBLoginView *fbButton = [[FBLoginView alloc] init];
    UIView * fbView = (UIView *)fbButton;
    fbView = [self.view viewWithTag:fbloginViewTAG];

    [fbView addGestureRecognizer:tapped];

    DLog(@"found View %@", fbView);
}

-(void)tapped {
    DLog(@"tapped");
}

-(void)FacebookLogin {

    // https://developers.facebook.com/docs/ios/login-ui-control/
    // https://developers.facebook.com/docs/ios/session/

    FBLoginView *loginView = [[FBLoginView alloc] init];
    loginView.delegate = self;
    loginView.tag = fbloginViewTAG;
    loginView.readPermissions = @[@"email", @"basic_info"];
    loginView.frame = CGRectOffset(loginView.frame,
                                   (self.view.center.x - (loginView.frame.size.width / 2)),
                                   ([[UIScreen mainScreen] bounds].size.height - 80));

    // change fb login button text
    UILabel *fblabel = [loginView subviews][1];
    fblabel.text = @"Connect  Facebook";

    DLog(@"loginview subviews: %@", [loginView subviews][1]);

    [self.view addSubview:loginView];
    [loginView sizeToFit];

}

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {

    [SVProgressHUD showWithStatus:@"Connecting with Facebook"];
    if (! self.isFirstFBLoginDone) {
    #warning simple workaround, but not solving the source problem of twiced call from loginViewFetchedUserInfo
        if ([[SingletonClass sharedInstance] hasCredentials] == NO) {
            [self.swebapi registerUser:user];
        } else {
            [self toSurroundView];
        }

        self.isFirstFBLoginDone = TRUE;

    } // isFirstFBLoginDone
}
@end


推荐答案

可能在代码中导致问题的一些事情 -

A few things that may be causing problems in your code --


  1. 设置 UIGestureRecognizer 的代理,以强制识别多个手势识别器,具体来说,同时识别这个新的手势识别器以及Facebook登录按钮的手势识别器),使用: code> gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer

  1. Set the UIGestureRecognizer's delegate in order to force the recognition of multiple gesture recognizers, specifically the simultaneous recognition of this new gesture recognizer alongside the Facebook login button's gesture recognizer) using: gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer

设置您的 UIGestureRecognizer s cancelsTouchesInView 属性为NO。

Set your UIGestureRecognizer's cancelsTouchesInView property to NO.

不要将指针初始化到 fbButton 作为 UIView ,您应该将 fbButton 声明为 FBLoginView 并将 UIView 转换为 FBLoginView

Instead of initializing your the pointer to the fbButton as a UIView, you should declare fbButton as a FBLoginView and cast the UIView into a FBLoginView.

(最重要的是)你正在添加 UITapG

(Most importantly) You're adding the UITapGestureRecognizer to the view when you should be adding it to the button.

更改:

[self.view addGestureRecognizer:tapped];

to:

[fbButton addGestureRecognizer:tapped];

这篇关于知道在ios中按下Facebook登录按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆