Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView and Cookies on Android

Tags:

java

android

I have an application on appspot that works fine through regular browser, however when used through Android WebView, it cannot set and read cookies. I am not trying to get cookies "outside" this web application BTW, once the URL is visited by WebView, all processing, ids, etc. can stay there, all I need is session management inside that application. First screen also loads fine, so I know WebView + server interactivity is not broken.

I looked at WebSettings class, there was no call like setEnableCookies.

I load url like this:

public class MyActivity extends Activity {   @Override   public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);            WebView webview = new WebView(this);     setContentView(webview);           webview.loadUrl([MY URL]);   }   ..  } 

Any ideas?

like image 633
burak bayramli Avatar asked Apr 02 '10 11:04

burak bayramli


People also ask

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. The plan is, that every time APP plans to launch Webview, the cookies will need to be copied from the PersistentCookieJar to the CookieManager.

How do I enable cookies on WebView?

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

What is WebView in android?

Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.


2 Answers

If you are using Android Lollipop i.e. SDK 21, then:

CookieManager.getInstance().setAcceptCookie(true); 

won't work. You need to use:

CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true); 

I ran into same issue and the above line worked as a charm.

like image 197
Huzefa Gadi Avatar answered Sep 29 '22 07:09

Huzefa Gadi


From the Android documentation:

The CookieSyncManager is used to synchronize the browser cookie store between RAM and permanent storage. To get the best performance, browser cookies are saved in RAM. A separate thread saves the cookies between, driven by a timer.

To use the CookieSyncManager, the host application has to call the following when the application starts:

CookieSyncManager.createInstance(context) 

To set up for sync, the host application has to call

CookieSyncManager.getInstance().startSync() 

in Activity.onResume(), and call

 CookieSyncManager.getInstance().stopSync() 

in Activity.onPause().

To get instant sync instead of waiting for the timer to trigger, the host can call

CookieSyncManager.getInstance().sync() 

The sync interval is 5 minutes, so you will want to force syncs manually anyway, for instance in onPageFinished(WebView, String). Note that even sync() happens asynchronously, so don't do it just as your activity is shutting down.

Finally something like this should work:

// use cookies to remember a logged in status    CookieSyncManager.createInstance(this); CookieSyncManager.getInstance().startSync(); WebView webview = new WebView(this); webview.getSettings().setJavaScriptEnabled(true); setContentView(webview);       webview.loadUrl([MY URL]); 
like image 35
Pentium10 Avatar answered Sep 29 '22 05:09

Pentium10