8.7 released—WinterCG Compliance Part 1
Learn more

View on GitHub

@nativescript/social-share

Contents

Installation

cli
npm install @nativescript/social-share

Usage

Share an image

To share an image use the shareImage() function.

ts
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:

ts
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.

ts
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.

ts
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.

ts
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()

ts
shareImage(imageSource, options)

Allows you to share an ImageSource.

ParameterTypeDescription
imageSourceImageSourceThe image to share.
optionsShareOptionsOptional: An object providing more information about the image.

ShareOptions

PropertyTypeDescription
captionstringOptional: The caption to share alongside the image
subjectstringOptional: (Android-only)The subject of the share.
fileFormat'png' |'jpg'Optional: (Android-only)The generated image format. Defaults to 'jpg'.

shareText()

js
import { shareText } from '@nativescript/social-share'

shareText(text, subject)

Shares the specified text. Expects a simple string.

Parameters

ParameterTypeDescription
textstringThe text to share with the URL.
subjectstringOptional: (Android-only)The URL to share.

sharePdf()

js
import { sharePdf } from '@nativescript/social-share'

sharePdf(pdf, subject)

Used to share a PDF file.

Parameters

ParameterTypeDescription
pdfFileThe PDF file to share.
subjectstringOptional: (Android-only)The URL to share.

shareUrl()

js
shareUrl(url, text, subject)

Allows you to share a URL.

Parameters

ParameterTypeDescription
urlstringThe URL to share.
textstringThe text to share with the URL.
subjectstringOptional: (Android-only)The URL to share.

shareViaTwitter()

ts
async doShareTwitter() {
    await shareViaTwitter(text, url);
  }

Shares a text and/or a url via the Twitter app.

Parameters

ParameterTypeDescription
urlstringOptional: The URL to share.
textstringOptional: The text to share.

shareViaFacebook()

ts
async doShareFacebook() {
    await shareViaFacebook(text, url);
  }

Shares a text and/or a url via the Facebook app.

Parameters

ParameterTypeDescription
urlstringOptional: The URL to share.
textstringOptional: 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

  1. 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:
groovy
dependencies {
	implementation 'com.facebook.android:facebook-share:[5,6)'
}
  1. Add meta-data and provider sections to the AndroidManifest.xml file:
xml
<?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"
   			..>
  1. Create a file facebooklogin.xml in App_Resources/Android/src/main/res/values/. Add this to the file (replace the id):
xml
<?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