Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear cookies and cache of webview on Android when not in webview?

Upon a user's sign out from my app I am clearing everything that may have been cached previously from the webview by calling this method:

 public void clearCookiesAndCache(Context context){     CookieSyncManager.createInstance(context);     CookieManager cookieManager = CookieManager.getInstance();      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {         cookieManager.removeAllCookies(null);     }     else {         cookieManager.removeAllCookie();     }         } 

CookieSyncManager is marked as deprecated, however. CookieSyncManager.createInstance(context) is necessary to be called, however, if you have not loaded the webview previously. So how are we supposed to clear the cookies and cache without using the deprecated CookieSyncManager in cases where the webview may not have been previously loaded?

like image 351
JohnRock Avatar asked Mar 11 '15 22:03

JohnRock


People also ask

Does WebView have cache?

WebViews are android views, that when created, consume context . And if we cache activity context on the application level, we might end up leaking the activity that was used to create the web-view.

Can we cache WebView in android?

You can use the WebView cache to enable caching in WebView.

How do I clear cookies on WebView flutter?

To delete all cookies with CookieManager , create a CookieManager instance like the below first. import 'package:flutter/material. dart'; import 'package:webview_flutter/webview_flutter.


2 Answers

I use the following approach in my app:

    @SuppressWarnings("deprecation")     public static void clearCookies(Context context)     {          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {             Log.d(C.TAG, "Using clearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));             CookieManager.getInstance().removeAllCookies(null);             CookieManager.getInstance().flush();         } else         {             Log.d(C.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();         }     } 

Or in Kotlin

@SuppressWarnings("deprecation") fun clearCookies(context: Context?) {     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {         CookieManager.getInstance().removeAllCookies(null)         CookieManager.getInstance().flush()     } else if (context != null) {         val cookieSyncManager = CookieSyncManager.createInstance(context)         cookieSyncManager.startSync()         val cookieManager: CookieManager = CookieManager.getInstance()         cookieManager.removeAllCookie()         cookieManager.removeSessionCookie()         cookieSyncManager.stopSync()         cookieSyncManager.sync()     } } 

I call this method in the following manner from my fragment:

mWebView.clearCache(true); mWebView.clearHistory();      U.clearCookies(getActivity());      mWebView.loadUrl(authorizeURL); 

It is possible to dump the cookies for a domain before and after the call to clearCookies by

String yahooCookies = CookieManager.getInstance().getCookie("https://yahoo.com"); Log.d(C.TAG, "Cookies for yahoo.com:" + yahooCookies); 

After calling clearCookies yahooCookies will be null.

This implementation feeds my needs, I have tested it on several emulators and a prehistoric Samsung Galaxy Gio with Android 2.3.3 and Nexus 5 with Android 5.1.1.

like image 172
AdamVe Avatar answered Sep 19 '22 19:09

AdamVe


Working and tested ..

android.webkit.CookieManager cookieManager = CookieManager.getInstance();  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        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 cookieManager.removeAllCookie(); 
like image 24
Gayan Weerakutti Avatar answered Sep 19 '22 19:09

Gayan Weerakutti