39

In some cases in my application i need to log the user out, but i only have the FBSDKLoginButton instance in my view controller and implementing the

FBSDKLoginButtonDelegate

using

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)

I want to pop the login view after logging the user out (destroying Facebook session) Don't know how to use

FBSDKLoginManager.logOut(<#FBSDKLoginManager#>)

I'am working inside a UIViewController and referencing the FBSDKLoginButton through an outlet

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mina Kolta
  • 1,551
  • 2
  • 12
  • 22

5 Answers5

89

Ok, here is the thing for anyone needs assistance. I managed to log my users out using FBSDKLoginManager instance

Apparently and i don't know if is it documented or not FBSDKLoginManager watches

FBSDKAccessToken.currentAccessToken()

so

let loginManager = FBSDKLoginManager()
loginManager.logOut() // this is an instance function 

Logs the user out, what i misunderstood that logOut is not a class function.

starball
  • 20,030
  • 7
  • 43
  • 238
Mina Kolta
  • 1,551
  • 2
  • 12
  • 22
  • I am calling this from a tabbar controller and returning to my rootview controller i.e. my login in controller. When I return to this login view controller, I am seeing the log "user logged out", so the logout worked but I am getting into the function login didCompleteWithResult. Any idea why? – user2363025 Jul 08 '15 at 15:34
  • Make sure you are not calling the login functionality in viewDidAppear inside the loginViewController of yours, this didCompeleteWithResult block should be called only when login action is triggered! If that didn't help paste the code here – Mina Kolta Jul 08 '15 at 15:53
  • I don't have any code in a viewdidappear for that controller. Can we move to chat? – user2363025 Jul 08 '15 at 16:09
  • it's fine if you want to chat, create the room please. – Mina Kolta Jul 08 '15 at 20:18
  • 1
    will add `import FBSDKLoginKit`.. to 4.2 Version – marlonpya Mar 13 '17 at 22:03
  • Thanks guys! FB really needs to update their Swift documentation, it contains like 5% of the documentation needed... – Lucas P. Jun 14 '18 at 13:43
35

Swifty and simple answer for lazy ones like me:

FBSDKLoginManager().logOut()
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
  • 3
    The problem is that if you call this, once the user tries to log in again with Facebook it'll just say "You have already authorized {App Name}". How can the user log out of Facebook then switch accounts? – MarksCode May 04 '17 at 07:02
  • @MarksCode. https://stackoverflow.com/questions/36368510/how-to-logout-user-using-facebook-authentication-using-swift-and-ios – Jeff Jul 13 '17 at 20:12
  • Now its: LoginManager().logOut() – RJB Jul 13 '20 at 19:50
18

For Swift 3 , Facebook SDK 4.16.0: Using Facebook Swift SDK

LoginManager().logOut()
djs
  • 4,626
  • 2
  • 28
  • 38
JT501
  • 1,407
  • 15
  • 12
11

Does the same, but in Objective-C:

[[[FBSDKLoginManager alloc] init] logOut];

Do not forget imports: FBSDKCoreKit/FBSDKCoreKit.h & FBSDKLoginKit/FBSDKLoginKit.h

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • It's a bit ugly, isn't it? – Ricardo May 04 '15 at 15:14
  • You can add a temporary variable for holding LoginManager for later on calling logOut. But this is the shortest way if I just want to logout. In my opinion LoginManager should have to be implemented as a singleton class (because at the end is working in that way) and then call [[FBSDKLoginManager shared instance] logOut]. – Javier Calatrava Llavería May 04 '15 at 17:14
1

Also, if you have used Parse and the Facebook iOS SDK version >= 4.4.0 to sign up or log in a user via Facebook, please use instead:

PFUser.logOut()

It will log out the user, delete the session on the Parse back-end side (do not forget to enable Parse revocable session via your Parse app settings), plus it will also delete the Facebook session written on the user device's disk.

I hope this answer will help you guys.

King-Wizard
  • 15,628
  • 6
  • 82
  • 76