How to persist webview cookies between app executions?

夙愿已清 提交于 2020-05-14 05:27:10

问题


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? can't find anything also on the android developers guide.

Thank you


回答1:


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();
    }
}



回答2:


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



来源:https://stackoverflow.com/questions/45751626/how-to-persist-webview-cookies-between-app-executions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!