Facebook SDK WebView doesn't logout
Asked Answered
B

1

6

I'm developing an app, that will be installed on one device and every user will use my device to share an image.

They need to login on Facebook to share the image on their profile. After sharing the image, the app should logout from facebook in order to let the next user to login.

For this reason, I'm not using the Facebook Android app, but opted for the webview alternative. After a user login, it opens a WebView (on Chrome) with the login and ask username, password and then permission.

The user publish the image, and the app calls:

LoginManager.getInstance().logOut();

When the next user comes, clicks on login with Facebook, it opens a WebView which says that I've already accessed this app from that Facebook account so I can just click on continue - there isn't even a logout button.

This is because it uses the precedent user session, which is still logged in inside the WebView.

I would need a way to logout the user from the WebView. I'm using Facebook sdk 4.31.0.

I've tried to Clear Cookies using the method described in this answer, but dosen't work:

public static void clearCookies(Context context)
{

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        Log.d(TAG, "Using clearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
        CookieManager.getInstance().removeAllCookies(null);
        CookieManager.getInstance().flush();
    } else
    {
        Log.d(TAG, "Using clearCookies code for API <" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
        CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context);
        cookieSyncMngr.startSync();
        CookieManager cookieManager=CookieManager.getInstance();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncMngr.stopSync();
        cookieSyncMngr.sync();
    }
}

And then:

    WebView mWebView = new WebView(getApplicationContext());

    mWebView.clearCache(true);
    mWebView.clearHistory();
    clearCookies(getApplicationContext());

The user is still logged in on chrome. Even thought, this works on "Browser" app (available on older devices).

Brinkmanship answered 4/4, 2018 at 9:50 Comment(0)
B
3

I've solved by using this line on my LoginButton:

    facebookLoginButton.setLoginBehavior(LoginBehavior.WEB_VIEW_ONLY);

This forces the sdk to use a custom webview (which is different from web_only, that opens the default browser's view).

Than I can use the code of the question to logout at the end of the interaction.

Brinkmanship answered 4/4, 2018 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.