Ionic 4 sample app using Cordova SocialSharing plug-in
Ionic Social Sharing plug-in Sample App
We strongly recommend that you check our upgraded Ionic Social Sharing plug-in Sample App created with Ionic here!
Introduction
In this sample app tutorial, we will show how this Ionic 4 app was built, where Cordova SocialSharing plug-in is used to share links, images, and text messages out of the app on social platforms or by email.
Creating App
Important note!
Note that the option of creating new apps with the Ionic 4 framework was removed and we no longer support the projects that were created with it earlier.
Moreover, you are free to effortlessly migrate your Ionic 4 project(s) to their Ionic version with keeping both apps active.
Please check this page to learn how to do it.
- From the Apps tab, click Create new app.
- Select Ionic 4 as the application type, enter sharingApp as the app name, and click Create.
- Then, inside the app, navigate to Project > App Settings > Cordova plugins. Go to the Core Plugins tab, scroll down the list of plug-ins and enable the SocialSharing plug-in:
- Click SAVE.
Adding Dependencies
Adding NPM Module
- Now, switch to Project > App settings > Npm modules and add a new Dependency: @ionic-native/social-sharing of version ^5.20.0
- Save the app changes by clicking the SAVE button at the top.
Importing Ionic Native Plug-in
- Unfold the Pages folder and choose the app page.
- Click the MODULE panel of the app page.
- In the Imports section, type in import { SocialSharing } from '@ionic-native/social-sharing/ngx';:
- Then, scroll down to the NgModule Settings > Providers section and type SocialSharing into the text field:
- Save the changes.
Building UI
- Navigate to Pages > Screen1 and select the DESIGN panel.
- Now, under the PROPERTIES panel, set the Page Footer property to False.
- Then, select the ToolbarTitle1 component and change its Text property to Sharing App:
- Create a new Text component, set its Text property to {{textToShare}} and the container property to p:
- Create a new Image element and set its src property to {{imageToShare}}. Alt. Text is set to Image error:
- Then, add a Button component to Screen1. Change its Text property to Share. To modify button appearance, unfold the Icon section, click the button next to the Style property and select share-alt icon in the popup menu. Set the icon's Slot property to Start.
- Clicking on the Share button will open an action sheet with sharing options. So, we need to add a button click event handler. Change the Button1 component's (click) property to share(), as in the screenshot below:
What is an Action Sheet?
An action sheet is a specific kind of alert that appears in response to a control or action and presents a set of two or more choices related to the current context. In our app, we'll use an action sheet to let users choose what app to use for sharing the content.
The app UI is ready, here's what you should get:
App Logic
Now we can move on to defining the app logic.
- Navigate to the Screen1 CODE panel.
- Under the Custom includes section, add a new dependency with the { SocialSharing } for Name and @ionic-native/social-sharing/ngx for its Path:
- Then, let's scroll down and define some necessary variables:
- variable socialSharing of SocialSharing type. The Add DI check box should be checked.
- variables urlToShare, imageToShare and textToShare of String type.
- Also, add the next values to the variables added (you can add your own values):
- urlToShare - 'https://appery.io/'
- imageToShare - 'https://appery.io/wp-content/uploads/nature-bg.jpg'
- textToShare - 'Hello Appery! (optional message, may be ignored by Facebook app)':
- Then, in the Functions section, create a new Method with share name. In the code editor, type the following code:
const options = {
message: this.textToShare,
subject: 'Share',
url: this.urlToShare,
files: [this.imageToShare]
};
this.socialSharing.shareWithOptions(options);
This is how the function should look like:
Save all the app changes.
App Testing
Congratulations! You are ready to test the app.
Congratulations! You are ready to test the app.
Note that to use the Sharing plug-in, you will need to run the app on the device.
Note as well that you will also need the apps (Twitter, Facebook, Instagram, email app, etc) to be installed on your mobile phone.
Appery.io Tester App
A great option to quickly test the app on the device is to use our Appery.io Tester app.
The app is free and available for both iOS and Android.
You can check out this page to learn more about using the Appery.io Tester app.
Click the Share button and choose one of the available options:
After selecting one of the available options, you will be redirected to the corresponding application (for example sending an email with Gmail).
Here is how the delivered sample email can look like in the Gmail inbox:
Troubleshooting
If you can only see the Cancel button in the action sheet, check if the necessary apps (email app, Facebook app, Instagram app, etc) are installed on your phone.
Plug-in Limitations
Facebook: sharing a text message is not possible (on both iOS and Android). You can share either a link or an image (not both), but a description cannot be prefilled.
Instagram does not allow you to prefill the message either. But you can offer users to paste the message that you passed to the plug-in because it will be added to the clipboard.
Further information on limitations can be found in this section of the plug-in documentation.
Want to know more?
You might also like this Appery.io Community video that shows how to create an Ionic 4 application using the Appery.io Social Sharing plug-in: