14

I previously used the following to clear and reset the Facebook access token

[FBSession.activeSession closeAndClearTokenInformation];

Since the update to 4.0 this no longer works. FBSession.activeSession has changed to [FBSDKAccessToken currentAccessToken].

I however can't find the latest version of closeAndClearTokenInformation that works with the latest version. Any suggestions?

Kerberos
  • 4,036
  • 3
  • 36
  • 55
memyselfandmyiphone
  • 1,080
  • 4
  • 21
  • 43

3 Answers3

37
FBSDKLoginManager *logMeOut = [[FBSDKLoginManager alloc] init];
[logMeOut logOut];

or

[FBSDKAccessToken setCurrentAccessToken:nil];
[FBSDKProfile setCurrentProfile:nil];

to logout

Then when you login again, make sure to set:

login.loginBehavior = FBSDKLoginBehaviorWeb;

Like so:

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
login.loginBehavior = FBSDKLoginBehaviorWeb;
[login logInWithReadPermissions:@[@"user_friends"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
         etc...
}];

I found some info in the docs saying FBSDKLoginBehaviorWeb can be used for "kiosk" apps; which I guess are apps designed to have more than one person log into them routinely.

One thing to note, this login method creates a modal UIWebView which is set up for portrait mode. I'm not sure if its possible to change this yet.

Brian Whipp
  • 495
  • 4
  • 8
  • 1
    Why would you include `login.loginBehavior = FBSDKLoginBehaviorWeb;` ? – memyselfandmyiphone Apr 07 '15 at 18:27
  • 1
    as a note the first option works without the need for `login.loginBehavior = FBSDKLoginBehaviorWeb;` . I think you have setup the SDK wrong if you need to use this additional line. – memyselfandmyiphone Apr 08 '15 at 18:42
  • the reason for `login.loginBehavior = FBSDKLoginBehaviorWeb;` is that when you use Browser mode, the session is shared with safari's session and you never get logged out until user logs out from safari. but if you use web mode, you will be successfully logged out. – mkhoshpour Mar 08 '16 at 14:46
  • I emphasize on `login.loginBehavior = FBSDKLoginBehaviorWeb;` to not share token with Safari app. Keep the token local to your app. – Nicolas Buquet Jun 28 '17 at 06:48
5

Swift 2+ Simple Solution

FBSDKLoginManager().logOut()

Swift 5+ Simple Solution

LoginManager().logOut()
fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
0

In swift you may want to do the following for close the session entirely:

import FacebookCore
import FacebookLogin

//...

//in your method:

let fbLoginManager = LoginManager()
fbLoginManager.logOut()
AccessToken.current = nil
UserProfile.current = nil
Omar
  • 1,005
  • 11
  • 11