如何知道用户何时解散React Native中的共享选项

问题描述 投票:1回答:2

我正在使用RN的Share API功能构建一个带有Expo的应用程序。我已成功实现以下内容以共享图像:

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

);

我想知道的是如何使用sharedAction()dismissedAction()选项。

基本上,我想知道用户是取消共享还是通过。

谢谢!

react-native
2个回答
4
投票

正如您可以从docs中读到的那样,Share.share()会返回一个Promise,如果用户共享或取消该对话框,则返回操作会显示。被解雇的行动仅适用于iOS,因此如果您的实施需要,您可能需要编写platform specific code

在iOS中,返回一个Promise,它将调用一个包含action,activityType的对象。如果用户解除了对话框,Promise仍将通过Share.dismissedAction操作解决,所有其他键未定义。

在Android中,返回一个Promise,它始终通过Share.sharedAction操作来解析。

所以你可以这样做,

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');
});

0
投票

bennygenels答案大多是正确的,但是share()返回一个resolves as an object的Promise,所以我们需要一些额外的花括号{}来使它实际工作:

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');
});
© www.soinside.com 2019 - 2024. All rights reserved.