Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 658
Jonathan Avatar asked Jun 21 '13 08:06

Jonathan


People also ask

How do I enable cookies on WebView?

How do I enable cookies in a webview? CookieManager. getInstance(). setAcceptCookie(true);

Does WebView android share cookies?

Webview properly, APP has to share cookies with Ms. Webview in the way she prefers, that is through the WebKit CookieManager.

How do I enable cache in WebView?

This example demonstrate about How to enable app cache for webview in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

You should consider that

CookieManager.getInstance().setAcceptCookie(true); 

doesn't work from lollipop(API21) and above. You should check and use appropriate function for that case:

if (android.os.Build.VERSION.SDK_INT >= 21) {         CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true); } else {      CookieManager.getInstance().setAcceptCookie(true); } 
like image 195
Misha Akopov Avatar answered Oct 09 '22 04:10

Misha Akopov


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?

like image 40
Integrating Stuff Avatar answered Oct 09 '22 02:10

Integrating Stuff