Issue
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 totrue
.setDomStorageEnabled
is also set totrue
.
Solution
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');
});
Answered By - jerome2710
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.