0

I'm creating an Android app embedding just a browser displaying a website, using WebView:

mywebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) { ... }
});
mywebView.loadUrl("http://www.example.com");

I noticed two strange things:

  • I get a message like "Cookies are disabled"

  • When I click on links that should display PDF files, nothing happens (but no error message)

How to enable features like cookies and opening (or at least downloading) PDF files?

Basj
  • 41,386
  • 99
  • 383
  • 673
  • You should have a look at this [post](https://stackoverflow.com/questions/1652850/android-webview-cookie-problem) – kPieczonka Jul 07 '17 at 23:11

1 Answers1

4

Here is a gist of a small app that demonstrates how to enable cookies in WebView and how to download/open a PDF file.

Note that the app demonstrates two ways of opening a PDF file. The first way opens the PDF within WebView; the second way opens the PDF in an external app if one is defined for PDF files.

It is important to remember that WebView does not offer full-blown browser-like support. From the documentation for WebView:

Basic usage

By default, a WebView provides no browser-like widgets, does not enable JavaScript and web page errors are ignored. If your goal is only to display some HTML as a part of your UI, this is probably fine; the user won't need to interact with the web page beyond reading it, and the web page won't need to interact with the user. If you actually want a full-blown web browser, then you probably want to invoke the Browser application with a URL Intent rather than show it with a WebView.

Google continues to refine WebView, so you will want to keep abreast of changes. In addition, Google continues to address the security of WebView and, for instance, will no longer support OAuth requests originating from a WebView. See this.

Community
  • 1
  • 1
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
  • 1
    Thanks. The PDF download thing works, but the cookies thing still doesn't. I tried everything: `CookieManager cm = CookieManager.getInstance();`, and then `cm.setAcceptCookie(true); cm.setAcceptThirdPartyCookies(mywebView, true);` either after the `mywebView.loadUrl()` or before, it's the same "Sorry but this browser doesn't accept cookies" :( (it's a Wordpress wp-login.php page) – Basj Jul 11 '17 at 09:56
  • 1
    @Basj Glad the PDF download is working. Are you getting the cookie error message from WordPress? Could it be something like [this](https://www.steckinsights.com/how-to-fix-a-cookies-blocked-error-on-wordpress-admin-dashboard/) going on? What is your test environment (API/device)? Have you tried accessing a different site that requires cookies to see if they are blocked there as well? – Cheticamp Jul 11 '17 at 10:18
  • @Basj Another thought: Check any browser that you might be using and make sure that cookies are turned on. In Chrome, look under "Settings." – Cheticamp Jul 11 '17 at 20:26
  • Your link in a previous comment (about Wordpress solution) was ... super useful @Cheticamp, and solved the cookies problem! Many thanks! – Basj Jul 11 '17 at 20:31
  • @Basj Good to hear. – Cheticamp Jul 11 '17 at 20:33