WebView and Cookies on Android
JavaAndroidJava Problem Overview
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?
Java Solutions
Solution 1 - Java
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.
Solution 2 - Java
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]);
Solution 3 - Java
I figured out what's going on.
When I load a page through a server side action (a url visit), and view the html returned from that action inside a Webview, that first action/page runs inside that Webview. However, when you click on any link that are action commands in your web app, these actions start a new browser. That is why cookie info gets lost because the first cookie information you set for Webview is gone, we have a seperate program here.
You have to intercept clicks on Webview so that browsing never leaves the app, everything stays inside the same Webview.
WebView webview = new WebView(this);
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url); //this is controversial - see comments and other answers
return true;
}
});
setContentView(webview);
webview.loadUrl([MY URL]);
This fixes the problem.
Solution 4 - Java
> My problem is cookies are not working "within" the same session. –
Burak: I had the same problem. Enabling cookies fixed the issue.
CookieManager.getInstance().setAcceptCookie(true);
Solution 5 - Java
CookieManager.getInstance().setAcceptCookie(true);
Normally it should work if your webview is already initialized
or try this:
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.setAcceptCookie(true);