Skip to content

Instantly share code, notes, and snippets.

@FokkeZB
Last active December 16, 2015 15:49
Show Gist options
  • Select an option

  • Save FokkeZB/5458506 to your computer and use it in GitHub Desktop.

Select an option

Save FokkeZB/5458506 to your computer and use it in GitHub Desktop.
Sharing lib for Titanium

I use this share.js library for sharing in apps.

It will be extended further over time with more options and fallbacks.

TODO

  • Apply customIcons to optionDialog fallback as well
  • Add Twitter URL schemes and web-intent fallbacks
  • Add Android intents fallbacks
  • Add app-rate option
  • Translate activityView options to optionDialog fallbacks
var btn = Ti.UI.createButton({
title: 'Share'
});
btn.addEventListener('click', function () {
require('share').share({
text: 'Text to share',
description: 'Alternative text used in Facebook feed-dialog',
url: 'http://url.to.share',
caption: 'Caption for the URL, used in Facebook feed-dialog',
image: 'http://url.to.share/image.png', // Can be local path too
// Applied to activityView (https://github.com/viezel/TiSocial.Framework#socialactivityview)
removeIcons: 'print,sms',
customIcons: [
{
title:"Custom Share",
type:"hello.world",
image:"pin.png"
},
],
// Applied to iPad activityPopover (first only) and optionDialog
view: btn,
rect: undefined,
animated: undefined,
// Applied to optionDialog
title: 'Share this item',
titleid: undefined,
androidView: undefined,
tizenView: undefined
});
});
var ios = (Ti.Platform.name === 'iPhone OS');
var ipad = (ios && Ti.Platform.osname === 'ipad');
if (ios) {
var Social = require('dk.napp.social');
}
var facebook_appid, facebook;
function share(args) {
if (ios && Social.isActivityViewSupported()) {
if (ipad) {
Social.activityPopover({
text: args.url ? (args.text ? args.text + ' ' + args.url : args.url) : args.text,
image: args.image,
removeIcons: args.removeIcons,
view: args.view
});
} else {
Social.activityView({
text: args.url ? (args.text ? args.text + ' ' + args.url : args.url) : args.text,
image: args.image,
removeIcons: args.removeIcons
});
}
return;
}
var options = ['Twitter', 'Facebook', L('Cancel')];
if (args.removeIcons && typeof args.removeIcons === 'string') {
var removeIcons = args.removeIcons.split(',');
var removeTwitter = removeIcons.indexOf('twitter');
if (removeTwitter >= 0) {
options.splice(removeTwitter, 1);
}
var removeFacebook = removeIcons.indexOf('facebook');
if (removeFacebook >= 0) {
options.splice(removeFacebook, 1);
}
}
if (options.length === 1) {
return;
}
var dialog = Ti.UI.createOptionDialog({
cancel: options.length - 1,
options: options,
title: args.title,
titleid: args.titleid,
androidView: args.androidView,
tizenView: args.tizenView
});
dialog.addEventListener('click', function (e) {
if (options[e.index] === 'Twitter') {
if (ios && Social.isTwitterSupported()) {
Social.twitter({
text: args.text,
image: args.image,
url: args.url
});
}
} else if (options[e.index] === 'Facebook') {
if (ios && Social.isFacebookSupported()) {
Social.facebook({
text: args.text,
image: args.image,
url: args.url
});
} else if (facebook_appid !== false) {
if (!facebook_appid) {
facebook_appid = Ti.App.Properties.getString('ti.facebook.appid', false);
if (facebook_appid === false) {
return;
}
if (Ti.version >= '3.1.0') {
facebook = require('facebook');
facebook.appid = facebook_appid;
} else {
Ti.Facebook.appid = facebook_appid;
}
}
if (facebook) {
facebook.dialog('feed', {
link: args.url,
caption: args.caption,
description: args.description || args.text,
picture: args.image
}, function (e) {
return;
});
} else {
Ti.Facebook.dialog('feed', {
link: args.url,
caption: args.caption,
description: args.description || args.text,
picture: args.image
}, function (e) {
return;
});
}
}
}
return;
});
if (ipad) {
dialog.show({
animated: args.animated,
rect: args.rect,
view: args.view
});
} else {
dialog.show();
}
return;
}
exports.share = share;
<property name="ti.facebook.appid">123456789</property>
<modules>
<module platform="android">facebook</module>
<module platform="iphone">facebook</module>
<module platform="iphone">dk.napp.social</module>
</modules>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment