Search code examples
androidwebview

How to enable cookies in android webview?


How do I enable cookies in a webview?

I tried to use

CookieManager.getInstance().setAcceptCookie(true);

just before calling WebView.loadUrl() and it doesn't work as I get an HTML page error from a website saying cookies need to be enabled.

How does cookieManager know which webview to enable cookies?

Say if I had an activity with two webviews in the screen and I only wanted one of those webviews to enable cookies, how is that possible using a CookieManager?

I feel like I am missing something. I could not find a method like webView.setCookieManager or Cookiemanager.setWebView(webview).


Solution

  • CookieManager.getInstance() is the CookieManager instance for your entire application. Hence, you enable or disable cookies for all the webviews in your application.

    Normally it should work if your webview is already initialized: http://developer.android.com/reference/android/webkit/CookieManager.html#getInstance()

    Maybe you call CookieManager.getInstance().setAcceptCookie(true); before you initialize your webview and this is the problem?