【问题标题】:Customizing react native share message based on app基于应用自定义 React Native 分享消息
【发布时间】:2019-03-03 07:28:58
【问题描述】:

我正在考虑在我的应用中使用 ReactNative 的分享组件:https://facebook.github.io/react-native/docs/share#share

我是否可以根据用户选择与之共享的应用自定义消息? 就像如果用户说通过 twitter 分享而不是通过电子邮件分享的较长消息,我想提供更短的消息。

谢谢!

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    是的,您可以,如果用户确实分享了,您只需要像这样使用result.activityType 来检查它是否已分享到 WhatsApp,例如:

    result.activityType === 'net.whatsapp.WhatsApp.ShareExtension'

    查看此小吃https://snack.expo.io/@neydroid/stack-overflow-test

    import * as React from 'react';
    import { Alert, Button, Text, View, Share, StyleSheet } from 'react-native';
    import { Constants } from 'expo';
    import AssetExample from './components/AssetExample';
    import { Card } from 'react-native-paper';
    
    export default class App extends React.Component {
      onShare = async () => {
        try {
          const result = await Share.share({
            message:
              'React Native | A framework for building native apps using React',
          })
    
          if (result.action === Share.sharedAction) {
            if (result.activityType && result.activityType === 'net.whatsapp.WhatsApp.ShareExtension') {
              // shared with WhatsApp
              Alert.alert(result.activityType);
            } else {
              // shared
            }
          } else if (result.action === Share.dismissedAction) {
            // dismissed
          }
        } catch (error) {
          alert(error.message);
        }
      };
    
      render() {
        return (
          <View style={styles.container}>
            <Button title="Something To Share" onPress={this.onShare}>Share</Button>
          </View>
    
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        paddingTop: Constants.statusBarHeight,
        backgroundColor: '#ecf0f1',
        padding: 8,
      }
    });
    

    【讨论】:

    • 谢谢,但这是要知道用户分享后,用户分享了哪个应用。我正在寻找一种根据应用类型向应用本身提供不同消息的方法。
    • @somesh 你能解决这个问题吗?我的团队也有同样的问题:(
    • 不,看起来这是 ReactNative 的限制。
    猜你喜欢
    • 2016-10-02
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    相关资源
    最近更新 更多