0

I have tried removing cookies and clearing history from WebView but cannot able to logout completely.

@Override
public void onPageFinished(WebView view, String url) {

    view.clearHistory();
    super.onPageFinished(view, url);
    String title = mWebView.getTitle();
    if (title != null && title.length() > 0) {
        mTitle.setText(title);
    }
    CookieSyncManager.createInstance(getContext());
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();
    cookieManager.setAcceptCookie(false);
    Log.d(TAG, "onPageFinished URL: " + url);
    mSpinner.dismiss();
}

and setting up WebView

private void setUpWebView() {
    mWebView = new WebView(getContext());
    mWebView.setVerticalScrollBarEnabled(false);
    mWebView.setHorizontalScrollBarEnabled(false);
    mWebView.setWebViewClient(new OAuthWebViewClient());
    mWebView.getSettings().setJavaScriptEnabled(true);
    WebSettings mWebSettings = mWebView.getSettings();
    mWebSettings.setSavePassword(false);
    mWebView.clearView();
    mWebView.loadUrl("about:blank");
    mWebView.loadUrl(mUrl);
    mWebView.clearCache(true);
    mWebView.clearHistory();
    mWebView.setLayoutParams(FILL);
    mContent.addView(mWebView);
    CookieSyncManager.createInstance(getContext());
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();
    //cookieManager.setAcceptCookie(false);
}

Am I doing anything wrong?

Please help me, Thanks in advance.

Komal12
  • 3,340
  • 4
  • 16
  • 25
Uma Achanta
  • 3,669
  • 4
  • 22
  • 49

2 Answers2

1

CookieSyncManager is marked as deprecated,

Here is the documentation

and here is the method

public abstract void removeAllCookies (ValueCallback callback)

In your case:

cookieManager.removeAllCookies(null);

Should be like this

android.webkit.CookieManager cookieManager = CookieManager.getInstance();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
           // for  >21 devices
           cookieManager.removeAllCookies(null);
     }
      else 
      {
        // for  <21 devices 
       cookieManager.removeAllCookie();
       }

If with "null" it doesn't work, then you have to use a callback*. like below

 android.webkit.CookieManager cookieManager = CookieManager.getInstance();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
           // for  >21 devices
           cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
             // a callback which is executed when the cookies have been removed
             @Override
             public void onReceiveValue(Boolean aBoolean) {
                   Log.d(TAG, "Cookie removed: " + aBoolean);
             }
           });
     }
      else 
      {
        // for  <21 devices 
       cookieManager.removeAllCookie();
       }

Hope this will help.

King of Masses
  • 18,405
  • 4
  • 60
  • 77
1

Use the below method to clear cookie for every login. removeAllCookie method was deprecated in API level 21. Use removeAllCookies(ValueCallback) instead.

// Clear cookie to prevent automatic login
  @SuppressWarnings("deprecation")
  private void clearCookie() {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          CookieManager.getInstance().removeAllCookies(null);
      } else {
          CookieManager.getInstance().removeAllCookie();
      }
  }

This should also work

WebSettings webSettings = webview.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);

webView.clearCache(true);
webView.clearHistory();
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73