Facebook SDK 4.0 IOS Swift Log a user out programmatically
Asked Answered
H

5

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

Headliner answered 31/3, 2015 at 16:57 Comment(0)
H
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.

Headliner answered 31/3, 2015 at 21:45 Comment(6)
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?Dissociation
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 hereHeadliner
I don't have any code in a viewdidappear for that controller. Can we move to chat?Dissociation
it's fine if you want to chat, create the room please.Headliner
will add import FBSDKLoginKit.. to 4.2 VersionEvulsion
Thanks guys! FB really needs to update their Swift documentation, it contains like 5% of the documentation needed...Sergo
P
35

Swifty and simple answer for lazy ones like me:

FBSDKLoginManager().logOut()
Prevalent answered 9/6, 2016 at 5:2 Comment(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?Alpestrine
@MarksCode. #36369010Kanter
Now its: LoginManager().logOut()Gabe
C
18

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

LoginManager().logOut()
Convention answered 9/10, 2016 at 8:14 Comment(0)
T
11

Does the same, but in Objective-C:

[[[FBSDKLoginManager alloc] init] logOut];

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

Titleholder answered 14/4, 2015 at 7:44 Comment(2)
It's a bit ugly, isn't it?Sledgehammer
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].Womenfolk
N
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.

Nedi answered 15/9, 2015 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.