Page tree
Skip to end of metadata
Go to start of metadata

This guide explains how to invoke the native sharing functions in iOS and Android.

What is Share?

Share is one of the important native functions for applications which let's you share a text, an image or a file with related applications. Since it requires some prerequisite knowledge, you are strongly recommended to read the articles about the file and image objects before using the Share function.
Share method can be reach under the Device class. It requires some parameters to work which are;

  • items : The content which will be shared. For Android, only first item will be handled if this parameter is an array. This can be:
    • For Android:
      • string
      • SMF.Bitmap instance
      • SMF.IO.File instance
    • For iOS:
      • string
      • SMF.Bitmap instance
      • SMF.IO.File instance
      • An array contains items above.
  • keys : key – value pairs for applications. For Android, it can be “subject” or “email” but for iOS it can be any key – value pairs. These keys will be ignored if the shared application does not accept keys.

  • exclude : An array for the list of services which will be excluded from the share menu and will not be displayed. This parameter only works for iOS devices. These services can be ;
    • Device.share.iOS.ActivityType.POSTTOFACEBOOK
    • Device.share.iOS.ActivityType.POSTTOTWITTER
    • Device.share.iOS.ActivityType.POSTTOWEIBO
    • Device.share.iOS.ActivityType.MESSAGE
    • Device.share.iOS.ActivityType.MAIL
    • Device.share.iOS.ActivityType.PRINT
    • Device.share.iOS.ActivityType.COPYTOPASTEBOARD
    • Device.share.iOS.ActivityType.ASSIGNTOCONTACT
    • Device.share.iOS.ActivityType.SAVETOCAMERAROLL
    • Device.share.iOS.ActivityType.ADDTOREADINGLIST
    • Device.share.iOS.ActivityType.POSTTOFLICKR
    • Device.share.iOS.ActivityType.POSTTOVIMEO
    • Device.share.iOS.ActivityType.POSTTOTEINCENTWEIBO
    • Device.share.iOS.ActivityType.AIRDROP
  • onSuccess: This callback event will be triggered after the share action is finished successfully.
  • onFailure: This callback will be triggered if the share action fails.

Now, try to use the share method. First, for image sharing let’s capture a photo;

In this example, we need a variable which helps us to use an image object globally. Let’s define a variable in app.js;

var img = null;

Now, add a TextButton and an image object into your project. We will use TextButton to capture an image for showing on the screen. Write the codeLines below to its onPressed event of TextButton.;

function textButton1OnPressed(e) {
	SMF.Multimedia.startCamera({
		cameraType : 1,
		resolution : 2,
		autoFocus : true,
		onStart : function () {},
		onCapture : function (e) {
			img = new SMF.Bitmap({
					imageUri : e.photoUri,
					onSuccess : function (e) {
						Image1.image = e.imageUri;
					},
					onError : function (e) {
						alert("Error: " + e.message);
					}
				});
		},
		onCancel : function (e) {
			alert("cancel")
		},
		onFailure : function () {}
	});

Above the code lines capture a photo when the textButton1 pressed and assign it to the image object. Now, add a new TextButton again to trigger the share event. Write the code lines below to onPressed event of second TextButton object;

Device.share({
	items : img,
	keys : {
		"subject" : "I shared an image for you"
	},
	onSuccess : function (e) {
		alert("Succesfully shared");
	},
	onFailure : function (e) {
		alert("Error: " + e.message);
	}
});

Now, if you run the project you can capture and share a photo easily. For example, if we want to share a gallery photo just change the code of the textButton1 onPressed event as below : 

Device.Media.pickFromGallery({
	type : [SMF.MediaType.IMAGE],
	onSuccess : function (e) {
		img = new SMF.Bitmap({
				imageUri : e.file,
				onSuccess : function (e) {
					Pages.page1.Image1.image = e.imageUri;
				},
				onError : function (e) {
					alert("Error: " + e.message);
				}
			});
	},
	onCancel : function (e) {
		alert("cancelled");
	},
	onError : function (e) {
		alert("failed");
	}
});

Share to Facebook

share method is accessible under the Facebook class. The share method allows you to share a string content to Facebook with your Facebook application.

First of all, you should create an app from the Facebook developer center. When you create the app, you will see your app in the dashboard. you will use App ID and App Secret in your project.

After that, you should enter Facebook App ID and App Secret into your application from the "project.json" which is under "Config" folder.

Now, enter the App ID and App Secret which is provided by the Facebook Developer Center.

Finally, you can write your share code like below;

The first parameter is the content which will be shared, the second one is the "success callBack" event and the third one is the "error callBack" event.

Facebook.share("https://www.smartface.io/",
	function (e) {
		alert("successfully shared.");
	},
	function (e) {
		alert("an error occured.");
	});

For more Facebook operations, please read the Facebook Plugin document. Also, if you want to use "Facebook.share" method you should use iOS publish, it cannot work with iOS Emulator.

Share to Twitter

share method is accessible under the Twitter class. The share method allows you to share a string content to Twitter with a Twitter application.

First of all, you should create an app from the Twitter app center. When you created the app, see the details of your app. Open the “Keys and Access Tokens” tab. You will use the Consumer Key (API Key) and Consumer Secret (API Secret) .

After that, you should enter the Twitter Consumer Key and Consumer Secret into your application from the "project.json" which is under Config folder.

Now, enter the Consumer Key and Consumer Secret which is provided by the Application Center of Twitter.

Finally, you can write your share code as below:

The first parameter is the content which will be shared, the second one is the wrapping option if the text is longer than Twitter accepts, the third one is the "success callBack" event and the fourth one is the "error callBack" event.

Twitter.share("https://www.smartface.io/", true,
	function (e) {
		alert("successfully shared.");
	},
	function (e) {
		alert("an error occured.");
	});