Preview 2.0 is now in Public Beta!
Read the Announcement

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

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

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

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

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


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


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:
dependencies {
	implementation 'com.facebook.android:facebook-share:[5,6)'
}
  1. Add 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"
   			..>
  1. Create a file 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>

Demo App

Try the plugin demo here on StackBlitz.

License

Apache License Version 2.0