react-native-share-menu
react-native-share-menu copied to clipboard
Other packages not working in custom ios share extension
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.
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?
I found a solution for new NativeEventEmitter() Android: https://stackoverflow.com/a/72516352/103778
This gets rid of the warnings
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) {
}
The side effect is that the sharing extension does not work, but the warning disappears.
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.
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 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.addNewShareListenerand teardown usinglistener.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.
@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...