简体   繁体   English

ios-Facebook登录委托方法不起作用

[英]ios-Facebook login delegate method isn't working

The goal is to jump to anotherViewController after user logged in but stay the original page when user cancelled the authorization. 目标是在用户登录后跳转到另一个ViewController,但在用户取消授权时保留原始页面。 I tried to use delegate method but doesn't work. 我尝试使用委托方法,但不起作用。 I probably don't fully understand how delegate works in Facebook SDK I think. 我想我可能不太了解委托在Facebook SDK中的工作方式。 I set appDelegate properly because the Facebook button works fine. 我正确设置了appDelegate,因为Facebook按钮可以正常工作。 Much appreciated for helping since I've been struggling this for a long time! 自从我为此奋斗了很长时间以来,感谢您的帮助! :) :)


//  MainViewController.h
#import  <UIKit/UIKit.h>
#import "AnotherViewController.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>

@interface MainViewController : UIViewController <FBSDKLoginButtonDelegate>

@property (weak, nonatomic) IBOutlet id<FBSDKLoginButtonDelegate> delegate;
@property (weak, nonatomic) IBOutlet FBSDKLoginButton *FBButton;


@end

//MainViewController.m

#import "MainViewController.h"

@interface MainViewController () <FBSDKLoginButtonDelegate>

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //set the delegate to self in order for the delegate protocol methods to be notified
    FBSDKLoginButton *loginBtn = [[FBSDKLoginButton alloc] init];
    loginBtn.delegate = self;

 if ([FBSDKAccessToken currentAccessToken]) {
        // User is logged in, do work such as go to next view controller.
        UIViewController *secondCV = [[UIStoryboard storyboardWithName:@"Main" bundle:NULL]instantiateViewControllerWithIdentifier:@"AnotherViewController"];

        [self presentViewController:secondCV animated:YES completion:nil];
    }
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error{
    if(!error){
        NSLog(@"You've Logged in");
        NSLog(@"%@", result);

       //I want to go to anotherViewController after they log in
        AnotherViewController *secondVC = [[AnotherViewController alloc] init];
        [self presentViewController: secondVC animated:YES completion: nil];

    }
}

-(void)loginButtonDidLogOut:(FBSDKLoginButton *)loginButton{

}

I found where the problem is now. 我发现了问题所在。 Since I declared another new FBButton in my .m file, and I set that (in this case, loginBtn) as delegate. 由于我在.m文件中声明了另一个新的FBButton,因此我将其设置为委托(在本例中为loginBtn)。 Instead, I should set FBButton I declared in .h file 相反,我应该设置在.h文件中声明的FBButton

self.FBButton.delegate = self; 

That solved the problem. 那解决了问题。 Wish this could help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM