Plugins
Social Share
@nativescript/social-share
Contents
Installation
npm install @nativescript/social-share
Usage
Share an image
To share an image use the shareImage() function.
import { shareImage } from "@nativescript/social-share"
import { ImageSource } from "@nativescript/core"
async doShareImage() {
const imageSrc = await ImageSource.fromUrl(
'https://thiscatdoesnotexist.com/'
);
shareImage(imageSrc);
}
You can optionally provide a second argument to add more information about the image:
shareImage(imageSrc, {
caption: 'Your favorite cat of all times',
subject: 'Some subject',
fileFormat: 'png',
})
Share a URL
To share a URL, use the shareUrl() function.
import { shareUrl } from '@nativescript/social-share'
shareUrl(
'https://www.nativescript.org/',
'Home of NativeScript',
'How would you like to share this url?'
)
Share via the Twitter App
To share something via the Twitter mobile application, use the shareViewTwitter() function.
import { shareViaTwitter } from '@nativescript/social-share'
shareViaTwitter('Home of NativeScript', 'https://www.nativescript.org/')
Share a PDF File
To share a PDF file, use the sharePdf function.
import { sharePdf } from '@nativescript/social-share'
let pdf = File.fromPath('~/path/to/myPdf.pdf')
sharePdf(pdf, 'How would you like to share this text?')
API
shareImage()
shareImage(imageSource, options)
Allows you to share an ImageSource
.
Parameter | Type | Description |
---|---|---|
imageSource | ImageSource | The image to share. |
options | ShareOptions | Optional: An object providing more information about the image. |
ShareOptions
Property | Type | Description |
---|---|---|
caption | string | Optional: The caption to share alongside the image |
subject | string | Optional: (Android-only )The subject of the share. |
fileFormat | 'png' |'jpg' | Optional: (Android-only )The generated image format. Defaults to 'jpg' . |
shareText()
import { shareText } from '@nativescript/social-share'
shareText(text, subject)
Shares the specified text. Expects a simple string.
Parameters
Parameter | Type | Description |
---|---|---|
text | string | The text to share with the URL. |
subject | string | Optional: (Android-only )The URL to share. |
sharePdf()
import { sharePdf } from '@nativescript/social-share'
sharePdf(pdf, subject)
Used to share a PDF file.
Parameters
Parameter | Type | Description |
---|---|---|
pdf | File | The PDF file to share. |
subject | string | Optional: (Android-only )The URL to share. |
shareUrl()
shareUrl(url, text, subject)
Allows you to share a URL.
Parameters
Parameter | Type | Description |
---|---|---|
url | string | The URL to share. |
text | string | The text to share with the URL. |
subject | string | Optional: (Android-only )The URL to share. |
shareViaTwitter()
async doShareTwitter() {
await shareViaTwitter(text, url);
}
Shares a text and/or a url via the Twitter app.
Parameters
Parameter | Type | Description |
---|---|---|
url | string | Optional: The URL to share. |
text | string | Optional: The text to share. |
shareViaFacebook()
async doShareFacebook() {
await shareViaFacebook(text, url);
}
Shares a text and/or a url via the Facebook app.
Parameters
Parameter | Type | Description |
---|---|---|
url | string | Optional: The URL to share. |
text | string | Optional: The text to share. |
Note
that text
will usually be suppressed due to Facebook security/abuse prevention, but the url will go through.
Android Only NOTE
- If you are already using the Facebook Share SDK in your project you likely do not have to add the following. If you are not using the sdk explicitly in your project yet, add to your
app.gradle
file:
dependencies {
implementation 'com.facebook.android:facebook-share:[5,6)'
}
- Add
meta-data
andprovider
sections to theAndroidManifest.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
...>
<application
android:name="com.tns.NativeScriptApplication"
..>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<provider android:authorities="com.facebook.app.FacebookContentProvider{your-facebook-appid}"
android:name="com.facebook.FacebookContentProvider"
android:exported="true"
tools:replace="android:authorities"/>
<activity
android:name="com.tns.NativeScriptActivity"
..>
- Create a file
facebooklogin.xml
inApp_Resources/Android/src/main/res/values/
. Add this to the file (replace the id):
<?xml version='1.0' encoding='utf-8' ?>
<resources>
<string name="facebook_app_id">126035687816994</string>
</resources>
Demo App
Try the plugin demo here on StackBlitz.
License
Apache License Version 2.0
- Previous
- Shared Notification Delegate
- Next
- SwiftUI