Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when user dismisses Share option in React Native

Tags:

react-native

I am building an app with Expo that utilizes RN's Share API feature. I have successfully implemented the following to share an image:

Share.share(
{
  message: 'This is a message',
  url: FileSystem.documentDirectory + imageUrlDate
},
{
  dialogTitle: 'Share Today',
  excludedActivityTypes: [
    'com.apple.mobilenotes.SharingExtension',
    'com.apple.reminders.RemindersEditorExtension'
  ]
}

);

What I would like to know is how to use the sharedAction() and dismissedAction() options.

Basically, I want to know whether a user cancels sharing or follows through.

Thanks!

like image 825
RonE Avatar asked Oct 15 '25 22:10

RonE


1 Answers

As you can read from the docs, Share.share() returns a Promise and the return action shows you if the user shared or dismissed the dialog. Dismissed action is only for iOS so you might need to write platform specific code if your implementation needs.

In iOS, Returns a Promise which will be invoked an object containing action, activityType. If the user dismissed the dialog, the Promise will still be resolved with action being Share.dismissedAction and all the other keys being undefined.

In Android, Returns a Promise which always be resolved with action being Share.sharedAction.

So you can do something like this,

Share.share({ message: 'This is a message', url: FileSystem.documentDirectory + imageUrlDate },
{
  dialogTitle: 'Share Today',
  excludedActivityTypes: [
    'com.apple.mobilenotes.SharingExtension',
    'com.apple.reminders.RemindersEditorExtension'
  ]
}).then(({action, activityType}) => {
  if(action === Share.dismissedAction) console.log('Share dismissed');
  else console.log('Share successful');
});
like image 175
bennygenel Avatar answered Oct 18 '25 23:10

bennygenel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!