Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FBSDKAccessToken currentAccessToken is not being updated after log in

I implemented a Facebook login button which works perfectly fine. However, after user successfully logs in into Facebook and goes back to the app, [FBSDKAccessToken currentAccessToken] returns NO. I added an observer to see if the code I want is running after returning back to the app:

-(void)viewDidLoad
{
   [[NSNotificationCenter defaultCenter] addObserver:self   selector:@selector(checkLogIn)name :UIApplicationWillEnterForegroundNotification object:nil];
}
-(void)checkLogIn
 {
         NSLog(@"IN CHECK LOG IN");

        if ([FBSDKAccessToken currentAccessToken]) {

            NSLog(@"Access Token Activated");

        }
 }

And after Log In when I return to the app the checkLogIn method is being called but [FBSDKAccessToken currentAccessToken] still returns NO. However, it changes to YES after I press home button on iPhone and go back to the app.

How can it be updated immediately, so I can show user another view controller?

UPDATE: I found out the way to execute proper code after getting back to the app. The key is to use FBSDKAccessTokenDidChangeNotification is Notification Center. So final result will be like this:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkLogIn) name: FBSDKAccessTokenDidChangeNotification object:nil];

That way token will be updated and it will work.

like image 323
SmartTree Avatar asked May 13 '15 03:05

SmartTree


1 Answers

You should use the following code to listen for FB access token changes, rather than listening in the delegate methods:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didUpdateFacebookAccessToken) name: FBSDKAccessTokenDidChangeNotification object:nil];
like image 101
adjwilli Avatar answered Sep 20 '22 06:09

adjwilli