【问题标题】:Share not working in react native android/ios?分享在本机 android/ios 中不起作用?
【发布时间】:2018-10-29 12:21:34
【问题描述】:

如何共享将在 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)我该怎么做?我错过了什么吗?

【问题讨论】:

  • @harsh Patel 你能帮我分享一下本机反应吗?为什么它不起作用?
  • 很抱歉,我还没有尝试过 react JS。

标签: javascript reactjs react-native


【解决方案1】:

Share.share(content, options)分别接收内容和选项参数。

Share.share(content, options) 在 Android 和 IOS。您基本上需要解决 Promise 或阅读响应 来自它。

像这样

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 返回一个包含action、activityType

的对象

如果用户关闭了对话框,Promise 将通过 action 是 Share.dismissedAction 否则 action 是 Share.sharedAction

【讨论】:

    【解决方案2】:

    阅读friendly share() docs

    内容

    • message - 要分享的消息

    • title - 邮件标题

    iOS

    • url - 要分享的网址

    URL 和消息中的至少一项为必填项。

    所以在 Android 上,url 选项什么都不做,您可能需要将其放入 message

    【讨论】:

      【解决方案3】:

      这对我有用:Link

      shareApp = () =>{
      
              let  text = 'Want more buzz around your photos on Insta, Facebook, Twitter, Whatsapp posts?\n\nLet\'s make your stories get more eyeballs..\nDownload TagWag App '
              if(Platform.OS === 'android')
                  text = text.concat('https://hackeruna.com')
              else
                  text = text.concat('http://itunes.apple.com/app/id1453977874')
      
              Share.share({
                  subject: 'Download TagWag App Now',
                  title: 'Download TagWag App Now',
                  message: text,
                  url:'app://tagwag',
      
              }, {
                  // Android only:
                  dialogTitle: 'Share TagWag App',
                  // iOS only:
                  excludedActivityTypes: []
              })
          }
      

      【讨论】:

      • 它对我有用..谢谢
      【解决方案4】:

      你必须为此处理承诺..

       onSharePress = (url) => {
          Share.share({
            title: 'Alert Title',
            message: url + '\nMessage goes here.'
          }).then((res) => console.log(res))
            .catch((error) => console.log(error))
        };
      

      【讨论】:

        【解决方案5】:

        喜欢在 WhatsApp 上分享文字

        Linking.openURL(whatsapp://send?text=${'hello whatsApp'});

        喜欢在 Google 上分享文字

        Linking.openURL('https://support.google.com/mail/community');

        在 React Native App 中打开你的联系人

        Linking.openURL('content://com.android.contacts/contacts');

        【讨论】:

          猜你喜欢
          • 2017-11-17
          • 2023-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多