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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…