Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios Facebook SDK v4.x access token null despite being logged in via FBSDKLoginButton

My facebook access token is null despite the fact that the button shows that I'm logged in. Anyone know why this would be?

From RootViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    self->login_button = [[FBSDKLoginButton alloc] init];
    self->login_button.center = self.view.center;
    [self.view addSubview:self->login_button];

    FBSDKAccessToken* access_token =[FBSDKAccessToken currentAccessToken];
    NSLog(@"Access Token, %@",access_token);
}

From ApplicationDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [self.window setRootViewController:[[RootViewController alloc] init]];

    [self.window makeKeyAndVisible];

    [self.window setBackgroundColor:[UIColor purpleColor]];
    [FBSDKLoginButton class];
    return [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
}

also my -ObjC linker flag is set

like image 281
Brooks Avatar asked Apr 03 '15 14:04

Brooks


3 Answers

The FBSDKApplicationDelegate needs to be called first to resolved cached tokens. Since you are setting the root view controller immediately, that calls your viewDidLoad before the FBSDKApplicationDelegate. Instead, you can move the FBSDKApplicationDelegate up:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.
  [FBSDKLoginButton class];
  BOOL r = [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];

  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

  [self.window setRootViewController:[[RootViewController alloc] init]];

  [self.window makeKeyAndVisible];

  [self.window setBackgroundColor:[UIColor purpleColor]];


  return r;
}
like image 191
Chris Pan Avatar answered Nov 14 '22 13:11

Chris Pan


I got same issue today, only because I missed one step:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {

    BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
                                                                  openURL:url
                                                        sourceApplication:sourceApplication
                                                               annotation:annotation];

    return handled;
}
like image 44
dichen Avatar answered Nov 14 '22 13:11

dichen


I have had an issue where I could not get the Access Token because I am calling it before return [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];. I needed to check the access token to check whether the user is logged in or not to decide which should be my root view controller. What I did was I tried to get the access token from the cache manually in application didFinishLaunchingWithOptions by:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    FBSDKProfile *cachedProfile = [FBSDKProfile fetchCachedProfile];
    [FBSDKProfile setCurrentProfile:cachedProfile];

    FBSDKAccessToken *cachedToken = [[FBSDKSettings accessTokenCache] fetchAccessToken];
    NSLog(@"Cached Token: %@", cachedToken.tokenString);

    if (cachedToken) {
        //User is logged in, do logic here.
    }

    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                didFinishLaunchingWithOptions:launchOptions];
}

You can see this method by checking the implementation of - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in FBSDKApplicationDelegate.m

UPDATE

This does not seem to work anymore. I'm not sure why. But it seems that the fetchCachedProfile and accessTokenCache methods have been made internal. I could not access these methods anymore in my code as it gives me an error.

like image 1
SleepNot Avatar answered Nov 14 '22 12:11

SleepNot