'Share not working in react native android/ios?

How can I share pdf file link or image link which will open in react native mobile app web browser. I have added a share button but when the user clicks on it, it opens where to share menu options like WhatsApp, Gmail, messages, etc. But when clicking on WhatsApp it does not send any content why so? Do I need to make use of Gmail, WhatsApp API with my react native android app

code:

import {
  Text,
  View,
  StyleSheet,
  Button,
  Animated,
  Dimensions,
  ScrollView,
  Image,
  TouchableOpacity,
  Linking,
  Platform,
  Share
} from 'react-native';


// inside render

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

const shareOptions = {
   title: 'Download Brochure',
    url: brochure
}

// inside return 

<View style={{paddingLeft: 10, marginRight: 20, flex: 1, flexDirection: 'row', justifyContent: 'flex-end'}}>
    <TouchableOpacity onPress={() => this.onSharePress(shareOptions)}>
         <Icon style={{paddingLeft: 10}} name="md-share" size={25} color="black"/>
     </TouchableOpacity>
</View>

In screenshot below you can see it just opens the share options menu but when I click on some of the platform the content i.e the URL of the file is not sent how can I do that ? Am I missing something ?

enter image description here



Solution 1:[1]

Share.share(content, options) receives content and options parameter separately.

Share.share(content, options) returns a Promise on both Android and IOS. You basically need to resolve the Promise or read the response from it.

Like so

Share.share(
{
  message: 'Your message',
  url: YourURL
}
).then(({action, activityType}) => {
if(action === Share.sharedAction)
  console.log('Share was successful');
else
  console.log('Share was dismissed');
});

Promise returns an object containing action, activityType

If the user dismissed the dialog, the Promise will be resolved with action being Share.dismissedAction else with action being Share.sharedAction

Solution 2:[2]

Read the friendly share() docs:

Content

  • message - a message to share

  • title - title of the message

iOS

  • url - an URL to share

At least one of URL and message is required.

So on Android, the url option does nothing, and you probably need to put it into message.

Solution 3:[3]

This is works for me: Link

shareApp = () =>{

        let  text = 'Want more buzz around your photos on Insta, Facebook, Twitter, Whatsapp posts?\n\nLet\'s make your stories get more eyeballs..\nDownload TagWag App '
        if(Platform.OS === 'android')
            text = text.concat('https://hackeruna.com')
        else
            text = text.concat('http://itunes.apple.com/app/id1453977874')

        Share.share({
            subject: 'Download TagWag App Now',
            title: 'Download TagWag App Now',
            message: text,
            url:'app://tagwag',

        }, {
            // Android only:
            dialogTitle: 'Share TagWag App',
            // iOS only:
            excludedActivityTypes: []
        })
    }

Solution 4:[4]

You have to handle promise for this ..

 onSharePress = (url) => {
    Share.share({
      title: 'Alert Title',
      message: url + '\nMessage goes here.'
    }).then((res) => console.log(res))
      .catch((error) => console.log(error))
  };

Solution 5:[5]

like share text on WhatsApp

Linking.openURL(whatsapp://send?text=${'hello whatsApp'});

like share text on Google

Linking.openURL('https://support.google.com/mail/community');

open your Contact in React Native App

Linking.openURL('content://com.android.contacts/contacts');

Solution 6:[6]

the link is sending as a plaintext in whatsapp but it is working in telegram as a link.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Thomas
Solution 3 juanitourquiza
Solution 4 tahir mahmood
Solution 5 Chaurasia
Solution 6 Sayan Dey