Facebook SDK 4.0 IOS Swift Log a user out programmatically

30,089

Solution 1

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.

hope that helps someone!

Solution 2

Swifty and simple answer for lazy ones like me:

FBSDKLoginManager().logOut()

Solution 3

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

LoginManager().logOut()

Solution 4

Does the same, but in Objective-C:

[[[FBSDKLoginManager alloc] init] logOut];

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

Solution 5

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.

Share:
30,089
Mina Kolta
Author by

Mina Kolta

Father, Software Engineer, DevOps, Linux admin, Grammer

Updated on April 16, 2020

Comments

  • Mina Kolta
    Mina Kolta about 4 years

    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

  • Ricardo
    Ricardo about 9 years
    It's a bit ugly, isn't it?
  • Javier Calatrava Llavería
    Javier Calatrava Llavería about 9 years
    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].
  • user2363025
    user2363025 almost 9 years
    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?
  • Mina Kolta
    Mina Kolta almost 9 years
    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
  • user2363025
    user2363025 almost 9 years
    I don't have any code in a viewdidappear for that controller. Can we move to chat?
  • Mina Kolta
    Mina Kolta almost 9 years
    it's fine if you want to chat, create the room please.
  • marlonpya
    marlonpya over 7 years
    will add import FBSDKLoginKit.. to 4.2 Version
  • MarksCode
    MarksCode about 7 years
    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?
  • Jeff
    Jeff almost 7 years
  • Lucas P.
    Lucas P. about 6 years
    Thanks guys! FB really needs to update their Swift documentation, it contains like 5% of the documentation needed...
  • RJB
    RJB almost 4 years
    Now its: LoginManager().logOut()