Skip to content

A module for React Native that adds your app to the share menu of the device

License

Notifications You must be signed in to change notification settings

alyrik/react-native-share-menu

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-native-share-menu

Adds the application to the share menu of the device, so it can be launched from other apps and receive data from them.

The fork adds getSharedExtras method which receives all the extras from intent (not only TEXT). The extras are provided as a string (stringified object) but it may not be valid JSON so you must fix this string (e.g. remove \n) by yourself to parse it as JSON.

Installation

  • Install the module
npm i --save react-native-share-menu

Usage in Android

Automatic Installation (React Native 0.36+)

At the command line, in the project directory:

react-native link

Manual Installation

  • 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')
}
  • In android/app/src/main/AndroidManifest.xml in the <activity> tag:
<intent-filter>
   <action android:name="android.intent.action.SEND" />
   <category android:name="android.intent.category.DEFAULT" />
   <data android:mimeType="text/plain" />
</intent-filter>
  • Register module (in MainApplication.java)
import com.meedan.ShareMenuPackage;  // <--- import

public class MainApplication extends Application implements ReactApplication {
  ......
  @Override
  protected List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
      new MainReactPackage(),
      new ShareMenuPackage()  // <------ add here
    );
  }
  ......

}

Usage in iOS

In the share extension loadView() method, add:

NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
NSItemProvider *itemProvider = item.attachments.firstObject;
[ShareMenuModule setShareMenuModule_itemProvider:itemProvider];
[ShareMenuModule setContext: self.extensionContext];

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);

Or check the "example" directory for an example application.

How it looks

Releasing a new version

$ npm version <minor|major|patch> && npm publish

Credits

Sponsored and developed by Meedan.

About

A module for React Native that adds your app to the share menu of the device

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 52.8%
  • Java 22.4%
  • JavaScript 15.5%
  • Python 9.3%