簡體   English   中英

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

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

目標是在用戶登錄后跳轉到另一個ViewController,但在用戶取消授權時保留原始頁面。 我嘗試使用委托方法,但不起作用。 我想我可能不太了解委托在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{

}

我發現了問題所在。 由於我在.m文件中聲明了另一個新的FBButton,因此我將其設置為委托(在本例中為loginBtn)。 相反,我應該設置在.h文件中聲明的FBButton

self.FBButton.delegate = self; 

那解決了問題。 希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM