react-native-share-menu icon indicating copy to clipboard operation
react-native-share-menu copied to clipboard

Other packages not working in custom ios share extension

Open anmolg27 opened this issue 3 years ago • 2 comments

In my app i need, to access app groups shared file system from share modal which I made by following the docs for making custom ios view. For that i have used react-native-fs in react native code which isn't working but in the app this package works perfectly. I have also noticed that other packages are also not working in shared extension like react-native-async-storage. Seems like they haven't been imported. Any suggestions or guidance will be really helpful for me. I got this error when I use react-native-fs: Error: Requiring module "node_modules/react-native-fs/FS.common.js", which threw an exception: Invariant Violation: new NativeEventEmitter() requires a non-null argument.

anmolg27 avatar Jun 01 '22 09:06 anmolg27

When I call import ShareMenu from 'react-native-share-menu';

Android throws the following warnings:

 WARN  `new NativeEventEmitter()` was called with a non-null argument without the required `addListener` method.
 WARN  `new NativeEventEmitter()` was called with a non-null argument without the required `removeListeners` method.

Any idea why this only happens on Android?

visoft avatar Aug 20 '22 23:08 visoft

I found a solution for new NativeEventEmitter() Android: https://stackoverflow.com/a/72516352/103778

This gets rid of the warnings

visoft avatar Aug 27 '22 00:08 visoft

I can confirm that adding the below in ShareMenuModule.java removes the Android warning (probably should move the discussion elsewhere since this is for ios share). Does anyone know if this has side effects? At first glance, it does not look like it; we set up using listener = ShareMenuAPI.addNewShareListener and teardown using listener.remove().

  @ReactMethod
  public void addListener(String eventName) {

  }

  @ReactMethod
  public void removeListeners(Integer count) {

  }

devYonz avatar Apr 09 '23 15:04 devYonz

The side effect is that the sharing extension does not work, but the warning disappears.

ungarscool1 avatar Apr 10 '23 04:04 ungarscool1

No, it works fine on Android. Looking to see if this has issues like accumulating listeners forcing the system to kill the app, sort of like an OOM killer.

On iOS, i have an issue where it doesn't pick up the information if the app is in the foreground.

devYonz avatar Apr 10 '23 13:04 devYonz

On iOS I followed the instruction given in the root of the repository. But in your message you said:

I can confirm that adding the below in ShareMenuModule.java removes the warning. Does anyone know if this has side-effects?

I can tell you that I've done what you said, but it didn't work, I'm redirected to my app but nothing happen.

If it can help, I have this code in my App.tsx:

const handleShare = React.useCallback((item?: SharedItem) => {
    let url = '';
    if (!item) {
      return;
    }

    const {data} = item;
    if (!data || data.length === 0 || !data[0]?.data)
      return;

    for (const i of data) {
      if (i.data.match(/^https?:\/\/[^\s]+$/)) {
        url = i.data;
        break;
      }
    }
    // Do things
  }, []);

  React.useEffect(() => {
    ShareMenu.getInitialShare(handleShare);
  }, []);

  React.useEffect(() => {
    const listener = ShareMenu.addNewShareListener(handleShare);

    return () => {
      listener.remove();
    };
  }, []);

ungarscool1 avatar Apr 11 '23 20:04 ungarscool1

@ungarscool1 The modifications in ShareMenuModule.java only apply to Android. The share menu functionality works fine on my Android setup regardless of the modifications below. Forking the repo and adding those two functions is not worth the effort if it is just for silencing the new NativeEmitter ... warning messages. I would re-consider if had other side-effects

I can confirm that adding the below in ShareMenuModule.java removes the warning. Does anyone know if this has side-effects? At first glance, it does not look like it; we set up using listener = ShareMenuAPI.addNewShareListener and teardown using listener.remove().

  @ReactMethod
  public void addListener(String eventName) {

  }

  @ReactMethod
  public void removeListeners(Integer count) {

  }

I am still looking to understand why IOS is failing in the foreground.

devYonz avatar Apr 23 '23 22:04 devYonz

@anmolg27 Did you find a solution for other packages in custom ios share view?

I'm trying to display a pdf preview with react-native-pdf and that does not work in the ios custom view...

zabojad avatar Oct 20 '23 15:10 zabojad