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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With