# react-native-share React Native Share, a simple tool for share message and file to other apps.
Sponsors
If you use this library on your commercial/personal projects, you can help us by funding the work on specific issues that you choose by using IssueHunt.io!
This gives you the power to prioritize our work and support the project contributors. Moreover it'll guarantee the project will be updated and maintained in the long run.
Getting started
react-native
>= 0.60.0 please use react-native-share
>= 2.0.0
If you are using Automatic Way
yarn add react-native-share
or if you're using npm
npm install react-native-share --save
Important:
Linking is not needed anymore. react-native@0.60.0+
supports dependencies auto linking.
For iOS you also need additional step to install auto linked Pods (Cocoapods should be installed):
cd ios && pod install && cd ../
react-native
<= 0.59.10 please use react-native-share
<= 1.2.1:
If you are using If you are having any problems with this library, or need to use >= 2.0.0 please refer to: jetifier.
After installing jetifier, runs a npx jetify -r
and test if this works by running a react-native run-android
.
Automatic Way
yarn add react-native-share
react-native link react-native-share
or if you're using npm
npm install react-native-share --save
react-native link react-native-share
We recommend using the releases from npm, however you can use the master branch if you need any feature that is not available on NPM. By doing this you will be able to use unreleased features, but the module may be less stable. yarn:
yarn add react-native-share@git+https://git@github.com/react-native-community/react-native-share.git
Manual install
iOS Install
yarn add react-native-share
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-share
➜ios
and addRNShare.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNShare.a
to your project'sBuild Phases
➜Link Binary With Libraries
- In XCode, in the project navigator, select your project. Add
Social.framework
andMessageUI.framework
to your project'sGeneral
➜Linked Frameworks and Libraries
- In iOS 9 or higher, You should add app list that you will share.
If you want to share Whatsapp and Mailto, you should write
LSApplicationQueriesSchemes
in info.plistLSApplicationQueriesSchemeswhatsappmailto - (Optional) Also following lines allows users to save photos, add them in
info.plist
NSPhotoLibraryAddUsageDescription$(PRODUCT_NAME) wants to save photos - Run your project (
Cmd+R
)
iOS Install(using Pods)
If you wish, you can use cocopoads to use react-native-share.
You just need to add to your Podfile the react-native-share dependency.
# React-Native-Share pod pod 'RNShare', :path => '../node_modules/react-native-share'
After that, just run a pod install
or pod udpate
to get up and running with react-native-share.
Then run a react-native link react-native-share
, and doing the steps 6 and 7.
You can also see our example to see how you need to setup your podfile.
Btw, We also recommend reading this (amazing article)[https://shift.infinite.red/beginner-s-guide-to-using-cocoapods-with-react-native-46cb4d372995] about how pods and rn work together. =D
Android Install
-
yarn add react-native-share
-
Open up
android/app/src/main/java/[...]/MainApplication.java
- Add
import cl.json.RNSharePackage;
andimport cl.json.ShareApplication;
to the imports at the top of the file - Add
new RNSharePackage()
to the list returned by thegetPackages()
method
- Add
-
Append the following lines to
android/settings.gradle
:include ':react-native-share' project(':react-native-share').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-share/android')
-
Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-share')
-
(Optional) Follow this for implementing Provider
Windows Install
yarn add react-native-share
- In Visual Studio add the
RNShare.sln
innode_modules/react-native-share/windows/RNShare.sln
folder to their solution, reference from their app. - Open up your
MainPage.cs
app
- Add
using Cl.Json.RNShare;
to the usings at the top of the file - Add
new RNSharePackage()
to theList<IReactPackage>
returned by thePackages
method
Methods
open(options)
Open Simple share dialog
Returns a promise that fulfills or rejects as soon as user successfully open the share action sheet or cancelled/failed to do so. As a result you might need to further handle the rejection while necessary. e.g.
*For share multiple files, you must using option urls instead of url to share multiple files/images/docs. Example could be found in Example folder
Share ;
Supported options:
Name | Type | Description |
---|---|---|
url | string | URL you want to share (only support base64 string in iOS & Android ). |
urls | Array[string] | array of base64 string you want to share (only support iOS & Android). |
type | string | File mime type (optional) |
message | string | |
title | string | (optional) |
subject | string | (optional) |
excludedActivityTypes | string | (optional) |
failOnCancel | boolean | (defaults to true) Specifies whether promise should reject if user cancels share dialog (optional) |
showAppsToView | boolean | (optional) only android |
Url format when sharing a file
***Share base 64 file
When share a base 64 file, please follow the format below:
url: "data:<data_type>/<file_extension>;base64,<base64_data>"
***Share file directly
When share a local file directly, please follow the format below:
url: "file://<file_path>",
shareSingle(options) (in iOS & Android)
Open share dialog with specific application
This returns a promise too.
Supported options:
Name | Type | Description |
---|---|---|
url | string | URL you want to share |
type | string | File mime type (optional) |
message | string | |
title | string | (optional) |
subject | string | (optional) |
social | string | supported social apps: List |
forceDialog | boolean | (optional) only android. Avoid showing dialog with buttons Just Once / Always. Useful for Instagram to always ask user if share as Story or Feed |
NOTE: If both message
and url
are provided, url
will be concatenated to the end of message
to form the body of the message. If only one is provided it will be used
isPackageInstalled() (in Android)
It's a method that checks if an app (package) is installed on Android.
It returns a promise with isInstalled
. e.g.
Checking if Instagram is installed on Android.
Share
NOTE: in iOS you can use Linking.canOpenURL(url)
Static Values for social
These can be assessed using Share.Social property For eg.
; const shareOptions = title: 'Share via' message: 'some message' url: 'some share url' social: ShareSocialWHATSAPP;Share;
Name | Android | iOS | Windows |
---|---|---|---|
yes | yes | no | |
PAGESMANAGER | yes | no | no |
yes | yes | no | |
yes | yes | no | |
GOOGLEPLUS | yes | yes | no |
yes | yes | no | |
yes | no | no | |
SMS | yes | no | no |
SNAPCHAT | yes | no | no |
MESSENGER | yes | no | no |
yes | no | no |
How it looks:
Android | IOS | Windows | |
---|---|---|---|
Simple Share | |||
UI Component | TODO |
Troubleshooting
Share Remote PDF File with Gmail & WhatsApp (iOS)
When sharing a pdf file with base64, there are two current problems.
- On WhatsApp base64 wont be recognized => nothing to share
- In the GmailApp the file extension is wrong (.dat).
Therefore we use this "workaround" in order to handle pdf sharing for iOS Apps to mentioned Apps
- Install react-native-fetch-blob
- Set a specific path in the RNFetchBlob configurations
- Download the PDF file to temp device storage
- Share the response's path() of the donwloaded file directly
Code:
static { let filePath = null; let file_url_length = fileUrllength; const configOptions = fileCache: true path: DIRSDocumentDir + type === 'application/pdf' ? '/SomeFileName.pdf' : '/SomeFileName.png' // no difference when using jpeg / jpg / png / ; RNFetchBlob ;}
Nothing to do on Android. You can share the pdf file with base64
static { let filePath = null; let file_url_length = fileUrllength; const configOptions = fileCache: true ; RNFetchBlob ;}
Adding your implementation of FileProvider
-
applicationId
should be defined in thedefaultConfig
section in yourandroid/app/build.gradle
: -
File:
android/app/build.gradle
defaultConfig { applicationId "com.yourcompany.yourappname" ... }
-
Add this
<provider>
section to yourAndroidManifest.xml
File:
AndroidManifest.xml
-
Create a
filepaths.xml
under this directory:android/app/src/main/res/xml
.In this file, add the following contents:
File:
android/app/src/main/res/filepaths.xml
-
Edit your
MainApplication.java
class to addimplements ShareApplication
andgetFileProviderAuthority
-
The
getFileProviderAuthority
function returns theandroid:authorities
value added in theAndroidManifest.xml
file -
applicationId
is defined in thedefaultConfig
section of yourandroid/app/build.gradle
and referenced usingBuildConfig.APPLICATION_ID