2017-09-16 37 views
0

我正在利用RN的Share API功能构建一个具有世博的应用程序。我已成功实施以下操作以分享图像:如何知道用户何时退出React Native的共享选项

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()选项。

基本上,我想知道用户是否取消共享或跟随通过。

谢谢!

回答

1

正如您可以从docs读取的那样,Share.share()返回Promise,并且返回操作显示用户是否共享或取消对话。被驳回的行动仅适用于iOS,因此如果您的实施需要,您可能需要编写platform specific code

在iOS中,返回一个无极将包含要调用 动作,activityType一个对象。如果用户放弃了对话,那么Promise 仍然会被解决,其操作是Share.dismissedAction,并且所有 其他键都未定义。

在Android中,返回一个总是用动作 为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回答大多是正确的,但份额()返回一个承诺,其resolves as an object,所以我们需要一些额外的花括号{},使其实际工作:

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

添加一些细节到您的答案。 – Billa

相关问题