Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook SDK WebView doesn't logout

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).

like image 355
Federico Ponzi Avatar asked Apr 04 '18 09:04

Federico Ponzi


People also ask

Is Facebook Lite a WebView?

There's a good reason for that, too: it looks like Facebook Lite isn't much more than a WebView wrapper for the mobile site, plus native Android notifications.

How do I exit WebView?

If you have only 1 activity you can simply start the service and then call finish() on the activity. However, if you have multiple activities you have to make sure that you close them all (see also this post).

Is Facebook a WebView?

Both Facebook's Android app as well as Facebook's iOS app use a so-called in-app browser, sometimes also referred to as IAB.


1 Answers

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.

like image 109
Federico Ponzi Avatar answered Oct 11 '22 07:10

Federico Ponzi