Adding share action/extension in React Native

后端 未结 3 2076
长发绾君心
长发绾君心 2021-02-13 15:48

Trying to build a React Native app that injects a menu item in the Share menu (Share Action for Android, Share Extension for iOS) and receives shared items in the app. Is there

相关标签:
3条回答
  • 2021-02-13 16:19

    I implemented a module for that: https://www.npmjs.com/package/react-native-share-menu (currently just works for Android).

    That's how to use it:

    Install the module:

    npm i --save react-native-share-menu

    In android/settings.gradle:

    ...
    include ':react-native-share-menu', ':app'
    project(':react-native-share-menu').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-share-menu/android')
    

    In android/app/build.gradle:

    ...
    dependencies {
        ...
        compile project(':react-native-share-menu')
    }
    

    Register module (in MainActivity.java):

    import com.meedan.ShareMenuPackage;  // <--- import 
    
    public class MainActivity extends ReactActivity {
    ......
        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                new MainReactPackage(),
                new ShareMenuPackage()  // <------ add here      
            );
        }
    ......
    }
    

    Example:

    import React, {
        AppRegistry,
        Component,
        Text,
        View
    } from 'react-native';
    import ShareMenu from 'react-native-share-menu';
    
    class Test extends Component {
        constructor(props) {
            super(props); 
            this.state = {
                sharedText: null
            };
        }
    
        componentWillMount() {
            var that = this;
            ShareMenu.getSharedText((text :string) => {
                if (text && text.length) {
                    that.setState({ sharedText: text });
                }
            })
        }
    
        render() {
            var text = this.state.sharedText;
            return (
                <View>
                    <Text>Shared text: {text}</Text>
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('Test', () => Test);
    
    0 讨论(0)
  • 2021-02-13 16:20

    You can use the built in component: https://facebook.github.io/react-native/docs/actionsheetios.html#content

    Or you can use this component which can accept any view you want and makes it look like the iOS share component:

    https://github.com/eyaleizenberg/react-native-custom-action-sheet

    These are designed for iOS. For Android (and also iOS), you can use this: https://github.com/EstebanFuentealba/react-native-share

    0 讨论(0)
  • 2021-02-13 16:23

    You can use the new Share api introduced in 0.33. I actually have a video on how to do this here: http://codecookbook.co/post/how-to-share-text-from-your-react-native-app/

    0 讨论(0)
提交回复
热议问题