How to logout user using Facebook authentication using Swift and iOS?

If you to make the user log out from the app itself programmatically, you can check the following code.

let loginView : LoginManager = LoginManager()
loginView.loginBehavior = FBSDKLoginBehavior.Web

This will open the Facebook login popup in your app in which users can login into your app.

And for the logout, you can call:

  let manager = LoginManager()
  manager.logOut()

This will log out the user from the facebook in the app, after this if you call login method of SDK you will see the login popup again

If you want to clear the profile and token, also call the below code with logout.

 FBSDKAccessToken.setCurrentAccessToken(nil)
 FBSDKProfile.setCurrentProfile(nil)

When you call the logOut, the user is logged out of your app. As far as logging them out of Facebook, I don't think you can do that, nor would Facebook allow it. The first time a user authorizes you app through Facebook, Facebook adds your app to the list of apps that they are authorized with (they can access that page through Facebook.com). When they logOut and logIn again, they will see the page that you posted a picture of because they already authorized it. The only way for them to reauthorize themselves is to delete the app from their Facebook app page and log in to your app again.


The way the current Facebook SDK handles this seems like a security vulnerability to me. If I borrow a friends iPad and login to an app with Facebook when I logout I should be completely logged out of the app and the SFSafariViewController it used to authenticate me. Whereas right now it still remembers me in the SFSafariViewController using cookies.

To properly logout the user from both the Facebook SDK and SFSafariViewController I do the following:

    let fbLoginManager = LoginManager()
    fbLoginManager.logOut()
    let cookies = HTTPCookieStorage.shared
    let facebookCookies = cookies.cookies(for: URL(string: "https://facebook.com/")!)
    for cookie in facebookCookies! {
        cookies.deleteCookie(cookie )
    }

I really dislike this solution but it's the best I've been able to come up with.