13

I've spent an hours to find a way to send/share an image (and text if possible) into whatsapp app using react native,

I've read this question (in android) and this question (using linking)

on android, it is possible to send image and text to whatsapp, but on react native i don't see any way to archieve it,

anyone have an idea?

flix
  • 1,688
  • 3
  • 34
  • 64

3 Answers3

17

For react-native versions greater than 0.56.0 the social share functionality is already implemented in the library, so extra libraries like react-native-share are no longer needed and they could be unmantained. In fact, I was using react-native-share library some months ago for older versions of react-native and I migrated the corresponding code to the react-native implementation that exports a Share class that has a share method and it's very easy to use.

Then, you can use share method to share data and react-native will know what apps have the phone installed. In the following image you can see how the sharing screen looks like in an Android phone with WhatsApp application installed: enter image description here

And this is how it would like in an iOS simulator with no app installed: enter image description here

Here you can find an example of code:

import React, { Component } from 'react';
import {
  Share,
  Text,
  TouchableOpacity
} from 'react-native';

const shareOptions = {
  title: 'Title',
  message: 'Message to share', // Note that according to the documentation at least one of "message" or "url" fields is required
  url: 'www.example.com',
  subject: 'Subject'
};

export default class ShareExample extends React.Component {

  onSharePress = () => Share.share(shareOptions);

  render(){
    return(
      <TouchableOpacity onPress={this.onSharePress} >
        <Text>Share data</Text>
      </TouchableOpacity>
    );
  }
}

Finally you have to options to send the image + text message: - You can use the url field of the shareOptions adding the remote URI of the image so it can be previewed in the WhatsApp message, and the title or subject fields to add the text. - You can share a base64 file url like this: url: 'data:image/png;base64,<base64_data>'

Nacho Justicia Ramos
  • 8,313
  • 1
  • 16
  • 26
  • 3
    Double checking: Does the image share functionality works on Android as well? Looks not to me, since the url attribute is only available for iOS (as the documentation says). – demogar Jul 31 '18 at 19:40
  • You are right, you I think you could just add the image URL in the field `message` so WhatsApp will try to put the corresponding image resource in the message. – Nacho Justicia Ramos Jul 31 '18 at 20:23
  • 1
    Indeed, doable by using `Platform` to determine if Android or iOS (to use message or url). – demogar Aug 01 '18 at 16:25
  • @NachoJusticiaRamos I want to share pdf and jpeg files on whatsapp, gmail, etc. through react native app so in case of pdf and jpeg files what should be the changes required in `shareOptions` ? Will URL change if I am sending pdf or jpeg or png file ? – fun joker Oct 17 '18 at 11:54
  • 2
    @funjoker URL changes when sending different types of files. - For jpeg would be: url: "data:image/jpeg;base64," - For png: url: "data:image/png;base64," - And for pdf or other files you can do: url: "file://" – Nacho Justicia Ramos Oct 18 '18 at 08:01
  • @NachoJusticiaRamos Ok got it thanks your answer is very helpful. Upvoted :) – fun joker Oct 18 '18 at 08:07
  • Thanks!! Indeed, you can take a look at docs of react-native-community/react-native-share: https://github.com/react-native-community/react-native-share#url-format-when-sharing-a-file. Althought you should not use it because React-native already have this functionality, they explain pretty well in the Readme the **url** prop in the section "Url format when sharing a file". – Nacho Justicia Ramos Oct 18 '18 at 08:15
  • The Share API that comes with React Native (not the react-native-share library) is not able to show dataUrl as image. On Android, If I put the `dataUrl` for a qr code in `message`, it is just plain text in whatapp, gmail. I'd like to know how to show the actual image of the qr code. – RedGiant Jan 27 '19 at 00:21
  • @RedGiant I have same requirement. Did you get any solution? I am using deep link const whatsappDeepLink = 'https://wa.me/phoneNumber?text=Message'; Linking.canOpenURL(whatsappDeepLink) – Rajendar Manda Feb 06 '19 at 12:56
  • @user1915370 , I ended up using `react-native-share`. I use it to share images in whatsapp, gmail, twitter etc. The one bundled with React Native cannot attach images on Android. – RedGiant Feb 06 '19 at 15:12
  • Same issue for me image + message not sending in ios only android working fine – Rahul Mishra May 17 '19 at 10:10
  • Not working for me i tried both react-native-share and Share both but IOS whatsapp not working. – Rahul Mishra May 23 '19 at 11:05
  • can we send url & message together in iOS? – Gajendra Rawat Jul 05 '19 at 09:51
  • how can we send it to a specific number – Mufaddal Hamid May 20 '20 at 10:55
  • react-native-share is a terrible library. Poorly documented and not up to date. – Karanveer Singh Dec 07 '20 at 09:14
  • How to open WhatsApp directly? – Manjunath Gk May 06 '21 at 06:03
  • You can open a whatsapp conversation directly by doing `Linking.openURL('whatsapp://send?text=' + message + 'phone=' + phone);` – Nacho Justicia Ramos May 06 '21 at 16:13
6

I was using react native 0.59 version but still i was not able to share image and text(including a link) on whatsapp because the default react native share gets either message or url so it is necessary to use react-native-share library https://github.com/react-native-community/react-native-share . I also used rn-fetch-blob library to convert image url to base64 image data.

shareImage= () => {
RNFetchBlob.fetch('GET', `https://example.com/example.png`)
  .then(resp => {
    console.log('response : ', resp);
    console.log(resp.data);
    let base64image = resp.data;
    share('data:image/png;base64,' + base64image);
  })
  .catch(err => errorHandler(err));

share = base64image => {
  console.log('base64image : ', base64image);
  let shareOptions = {
    title: 'Title',
    url: base64image,
    message: 'https://somelink.com some message',
    subject: 'Subject'
  };

  Share.open(shareOptions)
    .then(res => {
      console.log(res);
    })
    .catch(err => {
      err && console.log(err);
    });
};

};

Farhan
  • 751
  • 1
  • 9
  • 17
  • does this works for android app also, because as per document URL is only allowed in IOS only – cauchy Jun 19 '19 at 02:14
  • Yes it works for android. The default react native share library only allows message or url but this library https://github.com/react-native-community/react-native-share allows both url and message(it is allowed for both ios and android in the docs), i have tested it on whatsapp on android and it worked. – Farhan Jun 19 '19 at 15:20
  • Works on android, didnt work on iOS. On iOS either image or text is sent – red-devil Jun 21 '22 at 10:28
0

You can use react-native-multi-share if you want share multiple picture at the same time :)

https://github.com/chuangbo/react-native-multi-share

Alex Lévy
  • 656
  • 1
  • 8
  • 15