navigator.share is not working in a WebView - android

Is there a reason why the navigator.share() JavaScript function is not working inside a WebView of my Android app? When accessing the same URL through the web browser, the native Android share dialog pops up, but not when accessing the URL from the WebView of my app.
The URL is using https.
The share action is user-triggered by an onClick.
setJavaScriptEnabled is set to true.
setDomStorageEnabled is also set to true.

Eventually, I used the following settings for my webviews. There is a bit more to it for me than just the navigator.share, but what works for me might work for others. Worth a try.
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAllowContentAccess(true);
mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
mWebView.addJavascriptInterface(new JavaScriptShareInterface(), "AndroidShareHandler");
The AndroidShareHandler will become a global JavaScript function which will be available in the webview and can be triggered by a button click for example:
Java:
package com.your.package;
import android.webkit.JavascriptInterface;
public class JavaScriptShareInterface {
#JavascriptInterface
public void share(String url) {
// your share action
}
}
JavaScript:
shareButton.addEventListener('click', () => {
window.AndroidShareHandler.share('https://stackoverflow.com');
});

Related

Android Webview onReceivedTitle not called on page reload

I've been trying to inject some js into a webview in my app. Have been using
onReceivedTitle method of WebChromeClient since I want to execute the js while the page is loading.
This has been working until now. But recently, I observed that onReceivedTitle is not called we reload the webpage, similar to window.location.reload.
Firstly, I can't understand why it shouldn't be called. Or it should be and it's a bug?
Secondly, now that we know it's not called, where else can I inject by js?
Thanks.
Apparently, this is not considered an Android bug, but rather a Chrome or Chromium-based WebView bug. It should be reported to http://crbug.com/.
I'll suggest you to try a workaround with custom WebViewClient:
You'll have to use a custom WebViewClient to get this done. You will override the onPageFinished() method so when a new page finishes loading you can set the webview to the appropriate title.
Below is a sample implementation of the above:
WebView mWebView = (WebView) findViewById(R.id.mwebview);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
ExperimentingActivity.this.setTitle(view.getTitle());
}
});
This answer here will prove to be useful:https://stackoverflow.com/a/8193479/9080948
Seems like this is a bug in chromium. I worked around this by injecting my scripts on onPageFinished method of WebviewClient.

Android Webview Page Loaded Event

I'm writing an SDK for android which needs to injects JavaScript code into webviews it finds by enumerating the UI.
After finding a webview, the code sets a WebViewClient by using setWebViewClient() and waits for onPageFinished() to perform the actual injection.
All was working well, until I tried to use this method on a Cordova 8 application.
The WebView used by Cordova 8 is a subclass (org.apache.cordova.engine.SystemWebView) that overrides the setWebViewClient() method in the following way:
public void setWebViewClient(WebViewClient client) {
this.viewClient = (SystemWebViewClient)client;
super.setWebViewClient(client);
}
Because of the casting to SystemWebViewClient, I get a cast exception.
I want my solution to be as generic as possible, so extending SystemWebViewClient is not an option, but I still need to know when the webview page is loaded in order to inject the JavaScript.
Is there any way to get a notification when a webview has finished loading a page without using WebViewClient and webView.setWebViewClient() ?
A potential workaround would be to use WebChromeClient instead, and override its onProgressChanged method as follows:
WebView webView = new WebView(this);
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
// Do your injection here
}
}
});

Webview not able to load https url in android?

I am implementing webview application in android. When i am trying to load https url one or two times it finishes the activity. Agian trying to load https url it shows webpage not available. please find below image what i got.
When i click on that url again, then it shows the websit.
I used the below code for loading the url.
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.facebook.com");
webView.clearView();
webView.measure(100, 100);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#SuppressLint("NewApi")
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
// this will ignore the Ssl error and will go forward to your site
handler.proceed();
error.getCertificate();
}
});
please any help guys.......
Thanks in advance
Try to use below attributes :
webView = (WebView) findViewById(R.id.webView1);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
Add internet settings in your manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
and check can you access internet on your device.
Delete this string:
super.onReceivedSslError(view, handler, error);
and in this method
public boolean shouldOverrideUrlLoading(WebView view, String url) {
turn return to false
like this:
return false;
It helped me
You should remove this
super.OnReceiveSslError(view,handler,error);
Add this overriding method to your WebViewClient implementation. You'll need to compile it with Android SDK 2.2 (API level 8) or later. The method appears in the public SDK as of 2.2 (API level 8) but we've tested it on devices running 2.1, 1.6 and 1.5 and it works on those devices too (so obviously the behaviour has been there all along).
WebView.setWebViewClient(new WebViewClient() {
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
handler.proceed();
}
});
One possibility here is a race condition.
You are loading https://www.facebook.com/ before setting up the WebViewClient, so there is a possibility that your implementation of OnReceivedSslError() will never get called if you get a quick enough response from facebook.
This would explain why it works for some people, not for others, and always works if the page is reloaded.
Also, I think you should just be returning false from shouldOverrideUrlLoading() if you want the page to load rather than attempting to reload the page - this might cause an infinite recursion / crash - possibly depending upon timing.
December 2016 answer:
If this happens only on certain devices with Android 5+ and only on certain pages, it's most likely due to this chromium bug:
https://www.chromium.org/developers/androidwebview/webview-ct-bug
The solution is to either:
tell customers to update webview to 55+ (might not be easy on all devices)
tell customers to move their system clock backward a few weeks (not a nice solution)
get a non-Symantec cert
Add your manifest file
<uses-permission android:name="android.permission.INTERNET" />
When ever you accessing web content need to get internet permission then only it will load.
Try this
WebView webview = (WebView)findViewById(R.id.webView);
Webview.setBackgroundColor(0);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://www.facebook.com");
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

Android : Is it possible to show jquery or javascript popup?

is it possible to show jquery or javascript popup message in android WebView ?
You can display popup message with the JavaScript alert(), confirm(), or prompt(). If they are not working then something else is wrong. Ensure your JavaScript is properly reaching those statements. Use console.log() to write messages to LogCat to trace your execution. Also make sure JavaScript is enabled. IE:
myWebView.getSettings().setJavaScriptEnabled(true);
You can simply use the line given below to show a alert in webview:
wv.loadUrl("javascript:alert('hello Android');");
If you want to make some changes in UI you can call javascript like given below in overridden function onPageFinished()
wv.setWebViewClient(new MyWebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
wv.loadUrl("javascript:document.getElementById('client').style.visibility ='hidden';");
}
}

WebView does not display loaded Html on some phones

I have a WebView in one of my Activities where I want to load a Html page. The page contains jquery-mobile and some html. So I do the following in my Activity :
mWebView=(WebView) findViewById(R.id.MyWebView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient(){
[...]
});
mWebView.loadUrl("http://www.mymobilepage.html");
The problem is that the page gets loaded and displayed on the emulator, and on a HTC Desire, but when I try to load it on a LG Optimus One nothing gets displayed. The events onPageStarted and onPageFinished both get fired in my WebViewClient but just a blank page is displayed, and also I don't have any errors in my LogCat.
Thanks in advance.
When onPageFinished is called, the page may not be completely rendered. The documentation states:
Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture).
However, note that onNewPicture is documented as deprecated and obsolete. I ask about a replacement/alternative here.
This should be a comment, but since there is a bit of code on it I've added as response.
Try changing default background to transparent and alerting as soon as the page is loaded, just to be sure that at least the html is being interpreted:
mWebView = (WebView) this.findViewById(R.id.webview);
mWebView.setBackgroundColor(0);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
mWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
view.loadUrl("javascript:(function() { alert('hello'); })()");
} });
and when loading the webpage:
mWebView.clearView();
mWebView.loadUrl("http://yourmobilepage.something/");
and let us know if something happened.
Try this:
webView.post(new Runnable() {
#Override
public void run() {
// Your code here...
}
});
Have you checked your html/js code with different versions on the emulator? Newer Android versions have newer versions of WebKit, that might be the problem.
I would also check if you have LogCat set to show Error messages only, or Debug+Info+Warning+Error messages. According to this, the javascript errors should show up as Debug messages.
I had a similar issue to this, I found that calling clearview and then reload seemed to clear it up -- as in:
mWebView.clearView();
mWebView.loadUrl("http://yourmobilepage.something/");
mWebView.reload();

Categories

Resources