I\'ve just updated facebook sdk v4.0
and according the tutorial of Using Custom Login UIs
-(IBAction)facebookLoginClick:(id)sender {
FBSDKLoginManag
Check the state of your FBSession:
isOpen
Indicates whether the session is open and ready for use.
@property (readonly) BOOL isOpen;
Declared In: FBSession.h
https://developers.facebook.com/docs/reference/ios/3.0/class/FBSession/
Actually, the reason is very simple, you can try following step to reappear this error code easily.
The "Facebook login fail" may have two reasons:
- you get the user info from facebook fail.
- you get the user info from facebook success,but upload to your own server fail.
The code in FBSDKLoginManager.m is:
- (void)validateReauthentication:(FBSDKAccessToken *)currentToken withResult:(FBSDKLoginManagerLoginResult *)loginResult
{
FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
parameters:nil
tokenString:loginResult.token.tokenString
HTTPMethod:nil
flags:FBSDKGraphRequestFlagDoNotInvalidateTokenOnError | FBSDKGraphRequestFlagDisableErrorRecovery];
[requestMe startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSString *actualID = result[@"id"];
if ([currentToken.userID isEqualToString:actualID]) {
[FBSDKAccessToken setCurrentAccessToken:loginResult.token];
[self invokeHandler:loginResult error:nil];
} else {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[FBSDKInternalUtility dictionary:userInfo setObject:error forKey:NSUnderlyingErrorKey];
NSError *resultError = [NSError errorWithDomain:FBSDKLoginErrorDomain
code:FBSDKLoginUserMismatchErrorCode
userInfo:userInfo];
[self invokeHandler:nil error:resultError];
}
}];
}
when currentToken.userID is not equal actualID, the FBSDKLoginUserMismatchErrorCode will throw.
Now, this issue will reappear when user A login facebook fail,but you do not have [FacebookSDKManager logOut] after the fail, the app will cache the accessToken for the user A , and then user A change facebook account to user B, when user B login facebook again,it will reappear this issue.
I had a similar problem.
After initialising FBSDKLoginManager I added a line to flush out the data and the (Facebook)Token:
FBSDKLoginManager *loginmanager= [[FBSDKLoginManager alloc]init];
[loginmanager logOut];
Hope this helps.
Thus, exactly as the OP asks, "am I missing something"?
Yes, the following standard example code which is seen everywhere, is simply wrong:
-(IBAction)facebookLoginClick:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
---- ONE MAGIC LINE OF CODE IS MISSING HERE ----
[login logInWithReadPermissions:@[@"email"]
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error) {...}
else if (result.isCancelled) {...}
else { // (NB for multiple permissions, check every one)
if ([result.grantedPermissions containsObject:@"email"])
{ NSLog(@"%@",result.token); }
}
}];
}
you must do this:
-(IBAction)facebookLoginClick:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logOut]; //ESSENTIAL LINE OF CODE
[login logInWithReadPermissions:@[@"email"]
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error) {...}
else if (result.isCancelled) {...}
else { // (NB for multiple permissions, check every one)
if ([result.grantedPermissions containsObject:@"email"])
{ NSLog(@"%@",result.token); }
}
}];
}
Otherwise, very simply, the app will not work if the user happens to change FB accounts on the device. (Unless they happen to for some reason re-install the app!)
Once again - the popular sample code above simply does not work (the app goes in to an endless loop) if a user happens to change FB accounts. The logOut call must be made.
You can use the following code to solve your problem :
[FBSDKAccessToken refreshCurrentAccessToken:^(FBSDKGraphRequestConnection *connection, id result, NSError *error){}
This happened to me when I changed the Facebook AppID (in the Info.plist file) switching form a test Facebook app to a production Facebook app. If your app sends the Facebook token to a server that generate a JWT for for instance, Facebook SDK still persists the fbToken (FBSDKAccessToken.currentAccessToken()) persisted unless it's told to remove it.
That may also happen in production when a user logs out of the app, a log out API request will log the user out and reset your JWT. But even before sending the log out API request, the client will need to tell the FBSDKLoginManager instance to log out.
if let currentAccessToken = FBSDKAccessToken.currentAccessToken() where currentAccessToken.appID != FBSDKSettings.appID()
{
loginManager.logOut()
}
For me in Objective-C following worked
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
if ([FBSDKAccessToken currentAccessToken]) {
[login logOut];
}
and issue was user was trying to login as another user as suggested by @Vineeth Joseph