4

Trying to build a React Native app that injects a menu item in the Share menu (Share Action for Android, Share Extension for iOS). Is there a component for this, and if not what's the best way to build one?

I don't need to create ActionSheet in my app, i need to injects a menu item in the Share menu to allow users share urls from browser to my app.

stkvtflw
  • 12,092
  • 26
  • 78
  • 155

1 Answers1

2

You will need to add some native code to your project. I don't have experience doing this for iOS, but here's an outline for how to do it on Android.

Simplifying the Android documentation's example, you should be able to write something like this to allow other apps to share a text string "into" your app:

AndroidManifest.xml

...
<activity android:name="<your_app>.MainActivity">
    <!-- Allow users to launch your app directly -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
    <!-- Allow other apps to "share" with your app -->
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>
...

MainActivity.java

...
@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get intent, action and MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    // Match the intent specified in AndroidManifest.xml
    if (Intent.ACTION_SEND.equals(action) && "text/plain".equals(type)) {
            handleSendText(intent); // Handle text being sent
    } 
}
...

If you write most of your application logic in Javascript using React Native, you'll probably want handleSendText to trigger a Javascript event — see the React Native docs on native modules for information about how to do this.

fagerbua
  • 406
  • 2
  • 9