Android WebView doesn't store the cookies - android

seems that Android WebView doesn't store the cookies, how do I enable them?
I used this code to test it:
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("http://www.w3schools.com/php/showphp.asp?filename=demo_cookie1");
After I reload (webView.reload()) the page I do see "Cookie 'user' is set!" but after I close the application and start it again I see "Cookie 'user' is NOT set!". Weirdly enough sometimes I do see it set when I first start the app. So what's going on here? Is there a delay when cookies are stored or am I missing something?
Thanks!

+1 for eXistenZ' answer, but now in 2020 CookieSyncManager is deprecated. Now you should use CookieManager.getInstance().flush() or write something like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().flush();
} else {
CookieSyncManager.getInstance().sync();
}
}
});

Seems that there is a delay indeed with the cookies so I have to use this code:
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
CookieSyncManager.getInstance().sync();
Toast.makeText(getApplicationContext(), "Page loading complete", Toast.LENGTH_LONG).show();
}
});
Now it works fine.

Related

android webView autofill username and password not working

I am passing username and password to the activity with webView. The problem is url loading is successful but i want to fill(autofill) username and password. I am a beginner, please help or give me any suitable github link.
WebSettings webSettings = superWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
superWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:document.getElementById('username').value = '"+userName+"';" +
"document.getElementById('password').value='"+password+"';");
}
});
superWebView.loadUrl(url);
use this webview settings
htmlWebView.getSettings().setAppCacheEnabled(true);
htmlWebView.getSettings().setAppCachePath("/data/data" + getPackageName() + "/cache");
htmlWebView.getSettings().setSaveFormData(true);
htmlWebView.getSettings().setDatabaseEnabled(true);
htmlWebView.getSettings().setDomStorageEnabled(true);
CookieManager.getInstance().acceptCookie();
replace htmlWebview With yourown webview
call javascript as a function instead of this.
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript: (function() {document.getElementById('username').value= '"+userName+"'; document.getElementById('password').value='"+password+"'; }) ();" );
}
Though the form must have "value" attribute.

Remove sign in button in google docs webview in android

I am showing PDF files by using google docs in WebView in android.
How to remove or hide "Sign In" button? I have attached screenshot below. Thanks in advance.
webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://docs.google.com/viewer?url=http://www.ex.com/terms.pdf");
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Add the embedded=true parameter.
webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://docs.google.com/viewer?embedded=true&url=https://orimi.com/pdf-test.pdf");
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Note: This answer was actually proposed by user3777879. Tested, and working, he should get the credit - Stuart
Try this
I have tried to many answers but did not get good answer.
Finally got the solution with adding few code in when loading the pdf into the webview .
final WebView wv_webview= (WebView) view.findViewById(R.id.wv_webview);;
wv_webview.getSettings().setJavaScriptEnabled(true);
wv_webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
wv_webview.loadUrl("javascript:(function() { " +
"document.querySelector('[role=\"toolbar\"]').remove();})()");
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
wv_webview.loadUrl("javascript:(function() { " +
"document.querySelector('[role=\"toolbar\"]').remove();})()");
}
});
String your_pdf_link="https://www.antennahouse.com/XSLsample/pdf/sample-link_1.pdf";
wv_webview.loadUrl("https://docs.google.com/viewer?embedded=true&url=" + your_pdf_link);
Note:- It will show only few milliseconds when pdf loads into webview
Output:-
webview.loadUrl("javascript:(function() { " +
"document.getElementsByClassName('drive-viewer-toolstrip')[0].style.visibility='hidden'; })()");
I'd self-host your terms document, and I'd host it as .html file format rather than .pdf.
If you do not have a domain in which to self-host the file, check other file hosting services and see if they offer a public option that won't request login. Potential services that may work for you include Mediafire, Cramitin, Hotfile, Rapidshare, etc. (this is not an ordered or comprehensive list, do your own search).

Android - Bug in Webview? Some URL's prompt webview to start external browser

I have a "NewsActivity" with a webview. When I start it from my main activity, links are opened in the webview correctly but, for some reason, some url's cause the webview to open but then to immediately launch the external browser. Checking the debug console I have not found any exception or other message thrown by webview as not being able to handle the url.
Please note that I am not talking about a link clicked after the webview has loaded the url/page.
I have also tried to activate javascript in the webview but to no avail.
Also, this happens for just some urls from the same domain (specifically a news website; also, I have no block url or override in place).
Here is one of the urls that fail to open in the webview: url_not_loaded
Here is the code that calls the "NewsActivity"
Intent intent = new Intent(this, NewsActivity.class);
intent.putExtra(MainActivity.EXTRA_MESSAGE, url);
startActivity(intent);
And here is the code in "NewsActivity"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
Intent intent = getIntent();
String url = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
mWebview = (WebView) findViewById(R.id.newsv);
Log.d(MainActivity.LOG_TAG, "URL: " + url);
mWebview.loadUrl(url);
}
If someone has a clue as to what may be happening or can suggest any idea, I'll be grateful.
Thanks!
Your URL's might have redirects. I encountered a similar problem.
Add this to your activity with the webview.
webView.setWebViewClient(new Callback());
Add this outside of onCreate.
private class Callback extends WebViewClient{ //Helps to open in webview instead of browser
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
It's not a bug.
You need to use WebViewClient.
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
System.out.println("hello");
return false;
}
});
//Toast.makeText(this, "", Toast.LENGTH_SHORT);
mWebView.loadUrl(url);
You can set a newWebViewClient like this:
webView.setWebViewClient(new WebViewClient());
But to be more precisely you need to override the shouldOverrideUrlLoading method like this:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}});

WebView displays a blank view for certain links

I'm using a webview:
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setGeolocationEnabled(true);
webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
loading.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
loading.dismiss();
super.onPageFinished(view, url);
}
});
For most links, it works just fine. But for, http://m.stubhub.com (and any of the same domain), it only shows a blank view (though it's finished loading the page). Are there settings, I'm missing or something?
Btw, there aren't any errors, as far as I can tell.
Suggestions/advice greatly appreciated. Thanks.
Did you tried onReceivedSslError ? :
public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
handler.proceed() ;
}
Let me know if that helps !
The solution was the permissions the site required. It uses HTML5 storage, so permission for DOMStorage had to be enabled...
webView.getSettings().setDomStorageEnabled(true);

shouldOverrideUrlLoading() never gets called

The shouldOverrideUrlLoading() never gets called if I click a link within the webview.
(It doesn't show the toast or logs anything). I've tried also onPageFinished and it doesn't get called too. I read other posts where users are having problems that this is not called only sometimes, but in my case it's completely ignored.
webview = new WebView(MyActivity.this);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setDisplayZoomControls(false);
webview.loadData(Html.getHtml(), "text/html", "UTF-8");
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
new M("shouldOverrideUrlLoading",getApplicationContext());
Log.v("ESSE3", "shouldOverrideUrlLoading()");
System.out.println(url);
System.out.println(Html.getHtml());
webview.loadData(Html.getHtml(), "text/html", "UTF-8");
return true;
}
});
webview.setWebViewClient(new WebViewClient());
setContentView(webview);
Tried with/without javascript enabled or returning true or false within the method.
You call setWebViewClient() twice. Once with your overriding method and once with an empty WebViewClient! That's why your method isn't getting called.
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
....
}
});
webview.setWebViewClient(new WebViewClient());

Categories

Resources