根据应用程序自定义React Native Share消息

时间:2019-03-01 23:01:32

标签: reactjs react-native

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

有没有一种方法可以根据用户选择与之共享的应用来自定义消息? 就像我想在用户说通过Twitter分享时提供一个简短的消息,而不是通过电子邮件分享时使用较长的消息一样。

谢谢!

1 个答案:

答案 0 :(得分: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,
  }
});