Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - can we share an image and text into whatsapp?

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?

like image 952
flix Avatar asked Mar 26 '18 04:03

flix


2 Answers

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>'

like image 68
Nacho Justicia Ramos Avatar answered Nov 03 '22 09:11

Nacho Justicia Ramos


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

};

like image 21
Farhan Avatar answered Nov 03 '22 09:11

Farhan