ios-Facebook login delegate method isn't working

2.2k Views Asked by At

The goal is to jump to anotherViewController after user logged in but stay the original page when user cancelled the authorization. I tried to use delegate method but doesn't work. I probably don't fully understand how delegate works in Facebook SDK I think. I set appDelegate properly because the Facebook button works fine. 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{

}
1

There are 1 best solutions below

0
On

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. Instead, I should set FBButton I declared in .h file

self.FBButton.delegate = self; 

That solved the problem. Wish this could help.