Share on iOS and Android

API Reference: Share

Share allows sending a text, image, file or contacts over other apps on the device. Blacklist works for iOS only.

Multiple Sharing

JavaScript
JavaScript
const FlexLayout = require('sf-core/ui/flexlayout');
const Share = require('sf-core/share');
const Image = require('sf-core/ui/image');
const File = require("sf-core/io/file");
const Application = require('sf-core/application');
const extend = require('js-base/core/extend');
const Contacts = require('sf-core/device/contacts');
const Page = require('sf-core/ui/page');
var Page1 = extend(Page)(
function(_super) {
_super(this, {
onShow: function(params) {
Application.statusBar.visible = false;
this.headerBar.visible = false;
},
onLoad: function(params) {
const page = this;
let image = Image.createFromFile("path to the image");
let file = new File({
path: "path to the file"
});
let text = "Hello from Smartface";
let contact = new Contacts.Contact();
contact.firstName = "Smartface";
Share.share({ items: [text, file, image, contact], page, blacklist: [Share.ios.Twitter, Share.ios.Facebook] });
}
});
this.layout.flexDirection = FlexLayout.FlexDirection.ROW;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
}
);
module.exports = Page1;

Contacts With Multi Type Files

We have recommend to use shareContacts method while sharing just contacts. Because of there are some application which support only a contact file at a time.

File Sharing

JavaScript
JavaScript
const Page = require("sf-core/ui/page");
const extend = require("js-base/core/extend");
const FlexLayout = require('sf-core/ui/flexlayout');
const Share = require('sf-core/share');
const File = require('sf-core/io/file');
var Page1 = extend(Page)(
function(_super) {
_super(this, {
onShow: function(params) {
Application.statusBar.visible = false;
this.headerBar.visible = false;
}
});
this.layout.flexDirection = FlexLayout.FlexDirection.ROW;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
var file = new File({
path: 'path to the file'
});
Share.shareFile(file, this, []);
}
);
module.exports = Page1;

Image Sharing

JavaScript
JavaScript
const Page = require("sf-core/ui/page");
const extend = require("js-base/core/extend");
const FlexLayout = require('sf-core/ui/flexlayout');
const Share = require('sf-core/share');
const Image = require('sf-core/ui/image');
var Page1 = extend(Page)(
function(_super) {
_super(this, {
onShow: function(params) {
Application.statusBar.visible = false;
this.headerBar.visible = false;
}
});
this.layout.flexDirection = FlexLayout.FlexDirection.ROW;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
var image = Image.createFromFile('path to the image');
Share.shareImage(image, this, []);
}
);
module.exports = Page1;

Text Sharing

JavaScript
JavaScript
const Page = require("sf-core/ui/page");
const extend = require("js-base/core/extend");
const FlexLayout = require('sf-core/ui/flexlayout');
const Share = require('sf-core/share');
var Page1 = extend(Page)(
function(_super) {
_super(this, {
onShow: function(params) {
Application.statusBar.visible = false;
this.headerBar.visible = false;
}
});
this.layout.flexDirection = FlexLayout.FlexDirection.ROW;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
Share.shareText("Hello from Smartface", this, [Share.ios.Twitter, Share.ios.Facebook]);
}
);
module.exports = Page1;

Only link can be shared to Facebook without its SDK.

Contacts Sharing

The following example picks a contact then share it through many applications.

JavaScript
JavaScript
const File = require("sf-core/io/file");
const Image = require("sf-core/ui/image");
const Share = require("sf-core/share");
const Page = require("sf-core/ui/page");
const Contacts = require("sf-core/device/contacts");
const Button = require("sf-core/ui/button");
const Application = require("sf-core/application");
const Color = require("sf-core/ui/color");
const Device = require('sf-core/device');
const extend = require("js-base/core/extend");
var Page1 = extend(Page)(
function (_super) {
var page = this;
_super(this, {
onShow: function (params) {
Application.statusBar.visible = false;
this.headerBar.visible = false;
}
})
var myButton = new Button({
marginTop: 250,
marginRight: 50,
marginLeft: 50,
height: 60,
text: "PICK CONTACT",
backgroundColor: Color.RED,
onPress: function () {
Contacts.pickContact({
page: page,
onSuccess: function (contact) {
console.log("Successfully picked ", contact);
shareContact(contact);
},
onFailure: function (e) {
console.log("Something went wrong ", e);
}
});
}
});
this.layout.addChild(myButton);
function shareContact(myContact) {
let result = true;
if (Device.System.OS === "Android") {
result = Application.android.checkPermission(Application.android.Permissions.READ_CONTACTS);
if (!result) {
let permissionCode = 1001;
Application.android.requestPermissions(permissionCode, Application.android.Permissions.READ_CONTACTS);
}
result = Application.android.checkPermission(Application.android.Permissions.READ_CONTACTS);
}
if (result) {
Share.shareContacts({ items: [myContact], fileName: `${myContact.firstName}${myContact.middleName}${myContact.lastName}`, page: page, blacklist: [Share.ios.Twitter, Share.ios.Facebook] })
}
}
}
);
module.exports = Page1;

Example: