javascript - 分享在 native android/ios 中不起作用?

标签 javascript reactjs react-native

我如何分享将在 React Native 移动应用 Web 浏览器中打开的 pdf 文件链接或图像链接。我添加了一个共享按钮,但是当用户单击它时,它会打开共享菜单选项的位置,例如 WhatsApp、Gmail、消息等。但是当单击 WhatsApp 时它不会发送任何内容,为什么会这样?我是否需要在我的 react native android 应用程序中使用 Gmail、WhatsApp API

代码:

import {
  Text,
  View,
  StyleSheet,
  Button,
  Animated,
  Dimensions,
  ScrollView,
  Image,
  TouchableOpacity,
  Linking,
  Platform,
  Share
} from 'react-native';


// inside render

onSharePress = (shareOptions) => Share.share(shareOptions);

const shareOptions = {
   title: 'Download Brochure',
    url: brochure
}

// inside return 

<View style={{paddingLeft: 10, marginRight: 20, flex: 1, flexDirection: 'row', justifyContent: 'flex-end'}}>
    <TouchableOpacity onPress={() => this.onSharePress(shareOptions)}>
         <Icon style={{paddingLeft: 10}} name="md-share" size={25} color="black"/>
     </TouchableOpacity>
</View>

在下面的屏幕截图中,您可以看到它只是打开了共享选项菜单,但是当我单击某些平台时,内容(即文件的 URL)未发送,我该怎么做?我错过了什么吗?

enter image description here

最佳答案

Share.share(content, options) receives content and options parameter separately.

Share.share(content, options) returns a Promise on both Android and IOS. You basically need to resolve the Promise or read the response from it.

像这样

Share.share(
{
  message: 'Your message',
  url: YourURL
}
).then(({action, activityType}) => {
if(action === Share.sharedAction)
  console.log('Share was successful');
else
  console.log('Share was dismissed');
});

Promise returns an object containing action, activityType

If the user dismissed the dialog, the Promise will be resolved with action being Share.dismissedAction else with action being Share.sharedAction

关于javascript - 分享在 native android/ios 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53045415/

相关文章:

javascript - 函数可以修改 Javascript 中的 'this' 对象吗?

javascript - 扩展 Ecma Script api

javascript - 正则表达式与 Javascript 匹配

reactjs - 如何从辅助类或函数访问 redux 存储

javascript - 如何从兄弟节点获取数据

reactjs - React 上下文 API 的选择器?

javascript - React 正在映射重复的帖子

reactjs - Uncaught TypeError : (0 , _reactTable.useTable) 不是函数

android - 如何在 Android 上以 mapbox-gl 样式将 `asset://` 用于非 APK 文件?

javascript - react 原生 : borderBottomWidth not working