npm install @nativescript/social-share
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'
})
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?'
)
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/')
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?')
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. |
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' . |
import { shareText } from '@nativescript/social-share'
shareText(text, subject)
Shares the specified text. Expects a simple string.
Parameter | Type | Description |
---|---|---|
text | string | The text to share with the URL. |
subject | string | Optional: (Android-only )The URL to share. |
import { sharePdf } from '@nativescript/social-share'
sharePdf(pdf, subject)
Used to share a PDF file.
Parameter | Type | Description |
---|---|---|
pdf | File | The PDF file to share. |
subject | string | Optional: (Android-only )The URL to share. |
shareUrl(url, text, subject)
Allows you to share a URL.
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. |
async doShareTwitter() {
await shareViaTwitter(text, url);
}
Shares a text and/or a url via the Twitter app.
Parameter | Type | Description |
---|---|---|
url | string | Optional: The URL to share. |
text | string | Optional: The text to share. |
async doShareFacebook() {
await shareViaFacebook(text, url);
}
Shares a text and/or a url via the Facebook app.
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
app.gradle
file:dependencies {
implementation 'com.facebook.android:facebook-share:[5,6)'
}
meta-data
and provider
sections to the AndroidManifest.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"
..>
facebooklogin.xml
in App_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>
Try the plugin demo here on StackBlitz.
Apache License Version 2.0