ios-Facebook登录委托方法无效

时间:2015-06-07 16:54:41

标签: ios objective-c iphone facebook delegates

目标是在用户登录后跳转到anotherViewController,但在用户取消授权时保留原始页面。我试图使用委托方法,但不起作用。我可能不完全理解委托如何在Facebook SDK中工作我认为。我正确设置了appDelegate,因为Facebook按钮工作正常。非常感谢帮助,因为我长期以来一直在努力奋斗! :)

//  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 个答案:

答案 0 :(得分:1)

我发现问题出在哪里。因为我在我的.m文件中声明了另一个新的FBButton,并将其设置为(在本例中为loginBtn)作为委托。相反,我应该设置我在.h文件中声明的FBButton

self.FBButton.delegate = self; 

解决了这个问题。希望这可以提供帮助。