navigator.share (Web Share API) is not working inside a Webview

See original GitHub issue

Bug description:

I have a <WebView> in my app built using Expo and React Native.

The webview opens a page that uses the Web Share API, i.e. navigator.share({ url: 'url' }).

However, navigator.share is not available inside the webview.

If the same page is opened within a normal browser, sharing works as expected.

To Reproduce:

Call navigator.share from inside a page opened in a webview and see that it’s undefined.

Expected behavior:

navigator.share should be defined and working.

Environment:

  • OS: Android
  • OS version: 8
  • react-native version: "react-native": "https://github.com/expo/react-native/archive/sdk-35.0.0.tar.gz",
  • react-native-webview version:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:19

github_iconTop GitHub Comments

11reactions
chihimngcommented, Oct 4, 2021

Solution by overriding navigator.share() to forward param to react native side for handling:

  1. Inject js before content loaded
// pass this in WebView injectedJavaScriptBeforeContentLoaded props
const injectedJavaScriptBeforeContentLoaded = `
if (navigator.share == null) {
  navigator.share = (param) => {
     window.ReactNativeWebView.postMessage('share:' + JSON.stringify(param));
  };
};
true;
`;
  1. Add onMessage callback to handle passed params, call react native’s Share to invoke native share sheet
import { Share } from "react-native"

interface WebShareAPIParam {
  // ref https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share
  url?: string;
  text?: string;
  title?: string;
  // files unhandled
}

// pass this in WebView onMessage props
const onMessage = useCallback(async (e: WebViewMessageEvent) => {
  const {data} = e.nativeEvent;
  if (data.startsWith('share:')) {
    try {
      const param: WebShareAPIParam = JSON.parse(data.slice('share:'.length));
      if (param.url == null && param.text == null) {
        return;
      }
      await Share.share(
        {
          title: param.title,
          message: [param.text, param.url].filter(Boolean).join(' '), // join text and url if both exists
          url: param.url,
        },
        {
          dialogTitle: param.title,
          subject: param.title,
        },
      );
    } catch (e: unknown) {
      console.error(e);
    }
  }
}, []);
10reactions
JKDoscommented, Feb 21, 2022

Still an issue in 2022. Did anyone find a solution not involving Java or Koplin?

Read more comments on GitHub >

github_iconTop Results From Across the Web

navigator.share is not working in a WebView - Stack Overflow
navigator.share is not working in a WebView · The URL is using https. · The share action is user-triggered by an onClick. ·...
Read more >
765923 - Web Share API doesn't work in Android WebView
Currently we are using presence of `navigator. share` to detect this feature which creates bugs (e.g. https://github.com/ampproject/amphtml/issues/11473) for ...
Read more >
Navigator.share() - Web APIs - MDN Web Docs
The navigator.share() method of the Web Share API invokes the native sharing mechanism of the device to share data such as text, URLs, ......
Read more >
Simple sharing on the web with navigator.share - Paul Kinlan
How to get this working. · Get Chrome Dev Channel on Android. · Go to any page on my blog and click the...
Read more >
WebView - Android Developers
To learn more about WebView and alternatives for serving web content, ... may be shared with other WebViews in the application, but is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found