How to persist webview cookies between app executions?
Asked Answered
H

2

8

It is possible to achieve this currently in Android? I only can find deprecated questions about old methods (CookieSynchManager) which not seems to work for this actually.

It is possible to achieve it? I can't find anything also on the Android Developers' Guide.

Hydrated answered 18/8, 2017 at 8:6 Comment(0)
S
11

Having the same problem...I solved reading the doc here:

https://developer.android.com/reference/android/webkit/CookieSyncManager

For future readers: to force the Cookie sync process you can manually call the flush() method of CookieManager

I personally put in webview the following code:

public class MyWebViewClient extends WebViewClient {

    public void onPageFinished(WebView view, String url) {
      CookieManager.getInstance().setAcceptCookie(true);
      CookieManager.getInstance().acceptCookie();
      CookieManager.getInstance().flush();
    }
}
Secretory answered 7/3, 2020 at 14:37 Comment(1)
After looking at numerous other answers that didn't work for me, this was the one that finally did. Thanks!Petrie
S
0

Since CookieSynchManager is deprecated, CookieManager.getInstance() is the CookieManager instance for your entire application. Hence, you enable it by

CookieManager.getInstance().setAcceptCookie(true)

setAcceptCookie(boolean accept);

Sets whether the application's WebView instances should send and accept cookies.

https://developer.android.com/reference/android/webkit/CookieManager.html

Sleek answered 18/8, 2017 at 8:42 Comment(4)
Hi, but these cookies are stored on RAM or will persist?Hydrated
@Hydrated It will persist unless the data/cache is cleared from App Info. Or in other case if we want to clear the cookies, we can clear it programatically.Sleek
@Manoj_Frekzz thank you but where can you find it? can't I find that description on the documentationHydrated
developer.android.com/reference/android/webkit/…Sleek

© 2022 - 2024 — McMap. All rights reserved.