Ionic Instagram Image Share

ionic-instagram-share

If your app works with images, chances are high your users want to post their output to their Instagram account. With Ionic, we can easily add this sharing function with a plugin and also add a special effect under the hood.

The Ionic Academy

More content, more detailed video courses and community help inside the member area.

Not an Ionic Academy Member? Click here to learn more

Also, if you are active on Instagram make sure to check out my profile where I post some behind the scenes images from time to time!

Starting our Instagram Image Sharing App

To start we create a blank new Ionic app and install the plugin for Instagram sharing as well as the Camera plugin so we can capture and image inside our app for testing. Go ahead and run:

After installing the plugins we need to make sure they are loaded correctly, therefore go to your src/app/app.module.ts and change it to:

Now we already got everything we need, as this is no special Instagram app that needs permissions we can easily open our images directly within the app through a sharing dialog.

Implement Image Capture and Instagram Share

Before we can post an image to Instagram we need to capture one. In our case our view will just display the current selected image along with 2 buttons to replace the image (or load the image in the beginning) and to then share the selected image to Instagram.

Open your src/pages/home/home.html and change it to:

Nothing special here, so let’s continue with the actual class.

Inside the loadImage() function we construct a little options object which we pass to the camera. In our case we set the options to select an image from our Photolibrary and also the result data to be a data URL which is needed for the Instagram plugin later. Once we get the data back we set it to our current image and add the needed string before the data to make it a real base64 image string!

Once we got the image set, we can easily share this image to Instagram with just one line. Additional we got a little feature here which automatically copies the second argument of the share() function to your clipboard.

The problem is that Instagram is not allowing to pre fill the caption block, so people got creative and you can now inform your users that some text was copied to their clipboard. This makes especially sense for apps with predefined texts, like when using something like Buffer!

In our case we just pass a dummy string for testing, and then there’s something to be aware of: If you reach the then() block of the promise this does not mean the user has shared the image!

The app can’t detect whether the image was really shared, this will only tell you that the Instagram app was opened. But the user still has the option to abandon that process, so keep that in mind.

For now, open your src/home/home.ts and change it to:

Now you are ready to offer your users easy Instagram sharing of their images. Perhaps you have already texts that make sense to pre fill the caption, or define some hashtags that will be always copied at that point.

You can also find a video version of this Quick Win below.