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
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:You can see this method by checking the implementation of
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
in FBSDKApplicationDelegate.mUPDATE
This does not seem to work anymore. I'm not sure why. But it seems that the
fetchCachedProfile
andaccessTokenCache
methods have been made internal. I could not access these methods anymore in my code as it gives me an error.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:
You could build the logic around waiting for
FBSDKAccessTokenDidChangeNotification
notification in your ViewController's code.In your
AppDelegate.m
:I got same issue today, only because I missed one step:
You should use tokenString instead, like this:
FBSDKAccessToken* access_token =[FBSDKAccessToken currentAccessToken].tokenString; NSLog(@"Access Token, %@",access_token);
It happened to me too, the "Login with Facebook" button was working as expected, I was being asked for permissions, allowed them, the
application:openURL:...
delegate method was being called with an URL that seemed valid, nonetheless theaccessToken
property remained nil.The problem in my case was the fact that somehow
Shared Keychain
was removed from the application entitlements, thus the Facebook SDK was not able to save the token in keychain. Enabling the capability solved the problem.The odd thing is that this was a silent error, so it was not clear from the beginning why the access token wasn't being set...