iOS Photo Sharing on Instagram using Titanium

Posted By : Akshay Luthra | 17-Dec-2015

This blog is written to provide a ready code in Titanium for sharing photo on Instagram app on iOS.

 

You need to have the Instagram app installed on your iOS device to share a photo in it.

To check the availabilty of Instagram app on your iOS device, you can use the module.

 

The code written in this blog will download the image from a photoURL (say, sent from your Server), will save the image on your device and then will share the image on native Instagram app (only if the Instagram app is installed on your device).

 

if (!Ti.Network.online) {
	alert("No Internet Connection");
	return;
} else {

	var dir = (!Ti.Android || !Ti.Filesystem.isExternalStoragePresent()) ? Ti.Filesystem.applicationDataDirectory : Ti.Filesystem.externalStorageDirectory;
	var localStorage = Ti.Filesystem.getFile(dir, "IMAGES_FOLDER");
	(!localStorage.exists()) && localStorage.createDirectory();
	var imageFilePath = Ti.Filesystem.getFile(localStorage.resolve(), 'image.png');

	var xhr = Ti.Network.createHTTPClient({
		onload : function(e) {

			if (this.status == 200) {
				imageFilePath.write(this.responseData);

				if (!Ti.Android) {

					var tigram = require('com.kosso.tigram');
					if (tigram.isInstalled) {
						var imageView = Ti.UI.createImageView({
							height : 500,
							width : 500,
							image : imageFilePath,
						});
						tigram.openPhoto({
							media : imageView.toImage()
						});
					} else {
						alert("Instagram app is not installed in your device!");
					}

				}
			} else {
				alert("Error occurred while sharing on Instagram");
			}
		},
		onerror : function(e) {
			alert("Unable to proceed due to slow Internet Connection");
		}
	});

	xhr.setTimeout(10 * 1000);
	xhr.open('GET', photoURL);
	xhr.send();

}
 

 

THANKS

About Author

Author Image
Akshay Luthra

Akshay Luthra has excellent experience in developing Cross Platform Mobile Apps using JavaScript and Titanium Framework. He loves listening to music and travelling new places.

Request for Proposal

Name is required

Comment is required

Sending message..