33

I am trying to logout from Facebook programmatically without using FBSDKLoginButton i had search how could I do i found this answer Can we logout facebook programatically but the problem is the FBSession is deprecated in new iOS FBSDK version

my question is Is there any way to clear the fb session in the new iOS FBSDK version? if there any way to logout from Facebook programmatically? or how could I call the logout action from FBSDKLoginButton

Thanking in advance :)

Community
  • 1
  • 1
Ahd Radwan
  • 1,090
  • 4
  • 14
  • 31

6 Answers6

89

You have two methods to logout. First, as suggested by Inder Kumar Rathore

FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logOut];

Second is by setting the currentAccessToken to nil

[FBSDKAccessToken setCurrentAccessToken:nil];

@cookiemonsta hope second method works for you.

mars
  • 1,651
  • 2
  • 15
  • 20
  • 2
    You should also call: [FBSDKProfile setCurrentProfile:nil] because logOut calls it too. Explained here: https://developers.facebook.com/docs/reference/ios/current/class/FBSDKLoginManager/ – Alan Scarpa Aug 03 '15 at 21:12
  • 1
    Yeah, sweet! Thanks :) – Mohsin Khubaib Ahmed Sep 05 '15 at 07:45
  • @mahieddine did you logout from Facebook? can you post the answer in swift? please help me – Ganesh Kumar Jan 08 '16 at 06:14
  • 2
    Facebook internally calls `[FBSDKAccessToken setCurrentAccessToken:nil]` in logout method, no need to call it explicitly. Mentioned in this doc: https://developers.facebook.com/docs/reference/ios/current/class/FBSDKLoginManager – atulkhatri Feb 09 '16 at 09:41
  • I see how the function works, but I guess it's a little bit strange that we need to create instance. Why they did not implement it as a static method. `setCurrentAccessToken` and `setCurrentProfile` are static as well =) – Matrosov Oleksandr Feb 18 '16 at 17:21
8

FBSDKLoginManager is your need, it has logOut method but you might have to use your custom login

e.g.

FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
  if (error) {
    // Process error
  } else if (result.isCancelled) {
    // Handle cancellations
  } else {
    // If you ask for multiple permissions at once, you
    // should check if specific permissions missing
    if ([result.grantedPermissions containsObject:@"email"]) {
      // Do work
    }
  }
}];

//then logout
[loginManager logOut];
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
8

Swift version:

FBSDKLoginManager().logOut()

You can use FBSDKLoginManager even if you logged in with FBSDKLoginButton.

Dulgan
  • 6,674
  • 3
  • 41
  • 46
Cmar
  • 141
  • 2
  • 7
3

For Swift 3 and 4

I would like to use the code mentioned over here, How to logout user using Facebook authentication using Swift and iOS?

where HardikDG mentioned a good answer for logout. what you need to do is add below line before the login happen,

fbLoginManager.loginBehavior = FBSDKLoginBehavior.web

and while logout use below code

FBSDKAccessToken.setCurrent(nil)
FBSDKProfile.setCurrent(nil)
FBSDKLoginManager().logOut()

this works perfectly for me.

Chetan
  • 881
  • 14
  • 22
1

Swift 3 and Swift 4:

import FacebookLogin
import FacebookCore

let loginManager = LoginManager()
loginManager.logOut()
DevB2F
  • 4,674
  • 4
  • 36
  • 60
0

The following would do the job.

import FBSDKLoginKit

LoginManager().logOut()
sadat
  • 4,004
  • 2
  • 29
  • 49