简体   繁体   中英

Objective-C : “receiver FBSDKAccessToken for class message is a forward declaration”

It is showing this error to me . All I did was download the Facebook SDK with CocoaPods and i'm trying to log in , but can not because of this mistake =(

FBSDKAccessToken and FBSDKGraphRequest are with this same error...

This is my code:

#import "TestesViewController.h"
#import "FBSDKCoreKit.h"
#import "FBSDKLoginKit.h"


@implementation TestesViewController

-(void) viewDidLoad {
    [super viewDidLoad];
    FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc]init];
    loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
    loginButton.center = self.view.center;
    [self.view addSubview:loginButton];
}
- (IBAction)loadButton:(id)sender {
    if ([FBSDKAccessToken currentAccessToken]) {
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 NSLog(@"fetched user:%@", result);
             }
         }];
    }
}
@end

Does changing

#import "FBSDKLoginKit.h"
#import "FBSDKCoreKit.h"

to

#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>

fix the issue? If not, there is likely a problem with the header search paths in your project/workspace.

Here you are using custom loadButton instead of FBSDKLogin button, so I think in order to use your custom FB login button your parameters cannot be nil, you need to provide parameter. Moreover, you need to first login user using login manager in order to get access token which would be nil if user had never logged in.Once you get access token you need to store it somewhere, probably in user defaults, so I am removing that part for now.Try changing the code to:

    FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithReadPermissions:@[@"email",@"user_birthday",@"public_profile"] fromViewController:nil handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
    // if ([FBSDKAccessToken currentAccessToken]) {
    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields":@"first_name, last_name, picture.type(large),email"}]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
         if (!error) {
             NSLog(@"fetched user:%@", result);
         }
     }];
    //}
}];

insert this code to your load button part. This is working for me. This might work for you. If you really want to make a custom FB login button, consider Custom FB button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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