Skip to content

Instantly share code, notes, and snippets.

@mitulbhalia
Forked from oroce/_for iOS users
Last active August 29, 2015 14:12
Show Gist options
  • Save mitulbhalia/862f88d52777e1b45452 to your computer and use it in GitHub Desktop.
Save mitulbhalia/862f88d52777e1b45452 to your computer and use it in GitHub Desktop.
This gist is only for Android.
If you would like launch native apps
on iPhone, iPad, you can find information about it in Appcelerator Docs:
-https://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Platform.canOpenURL-method.html
-https://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Platform.openURL-method.html
More info about iOS URL Schemes: http://handleopenurl.com/
function shareToFB(){
try{
var intFB = Ti.Android.createIntent({
action: Ti.Android.ACTION_SEND,
packageName: "com.facebook.katana",
className: "com.facebook.katana.ShareLinkActivity",
flags: 0x30000000,
type: "text/plain"
});
intFB.putExtra( Ti.Android.EXTRA_TEXT, "http://www.google.com"); //facebook only supports LINKS(!!!)
intFB.addCategory( Ti.Android.CATEGORY_LAUNCHER );
Ti.Android.currentActivity.startActivity( intFB );
}catch( x ){
//FB app is not installed, fallback to sg else
}
}
function shareToTwitter(){
try{
var intTwitter = Ti.Android.createIntent({
action: Ti.Android.ACTION_SEND,
packageName: "com.twitter.android",
className: "com.twitter.android.PostActivity",
flags: Ti.Android.FLAG_ACTIVITY_NEW_TASK,
type: "text/plain"
});
intTwitter.putExtra( Ti.Android.EXTRA_TEXT, "http://www.google.com with some message"); //twitter supports any kind of string content (link, text, etc)
Ti.Android.currentActivity.startActivity( intTwitter );
}catch( x ){
//Twitter app is not installed, fallback to sg else
}
}
function shareChooser(){
var intShare = Ti.Android.createIntent({
action: Ti.Android.ACTION_SEND,
type:"text/plain"
});
intShare.putExtra( Ti.Android.EXTRA_TEXT, "http://www.google.com with some message" );
Ti.Android.currentActivity.startActivity( intShare );
}
var app = {
sharer: {
fb: function( content ){
try{
var intFB = Ti.Android.createIntent({
action: Ti.Android.ACTION_SEND,
packageName: "com.facebook.katana",
className: "com.facebook.katana.ShareLinkActivity",
flags: 0x30000000,
type: "text/plain"
});
intFB.putExtra( Ti.Android.EXTRA_TEXT, "http://www.google.com"); //facebook only supports LINKS(!!!)
intFB.addCategory( Ti.Android.CATEGORY_LAUNCHER );
Ti.Android.currentActivity.startActivity( intFB );
}catch( x ){
//FB app is not installed, fallback to sg else
app.sharer.fallbackFB( content );
}
},
twitter: function( content ){
try{
var intTwitter = Ti.Android.createIntent({
action: Ti.Android.ACTION_SEND,
packageName: "com.twitter.android",
className: "com.twitter.android.PostActivity",
flags: Ti.Android.FLAG_ACTIVITY_NEW_TASK,
type: "text/plain"
});
intTwitter.putExtra( Ti.Android.EXTRA_TEXT, "http://www.google.com with some message"); //twitter supports any kind of string content (link, text, etc)
Ti.Android.currentActivity.startActivity( intTwitter );
}catch( x ){
//Twitter app is not installed, fallback to sg else
app.sharer.fallbackTwitter( content );
}
},
chooser: function( content ){
var intShare = Ti.Android.createIntent({
action: Ti.Android.ACTION_SEND,
type:"text/plain"
});
intShare.putExtra( Ti.Android.EXTRA_TEXT, "itten kontent" );
Ti.Android.currentActivity.startActivity( intShare );
},
fallbackFB: function( content ){
app.sharer._openWebViewWindow( String.format( "http://m.facebook.com/sharer.php?u=%s&t=%s", content, content ) );
},
fallbackTwitter: function( content ){
app.sharer._openWebViewWindow( String.format( "http://m.twitter.com/?status=%s", content ) );
},
_openWebViewWindow: function( url ){
var webViewWin = Ti.UI.createWindow({
modal: true
});
var webView = Ti.UI.createWebView({
url: url,
canGoBack: true,
canGoForward: true
});
webViewWin.add( webView );
webViewWin.open();
}
}
};
var MESSAGE = "http://google.com is great search engine";
var btnShareFB = Ti.UI.createButton({
title: "Share to native FB app"
});
btnShareFB.addEventListener( "click", app.sharer.fb.bind( null, MESSAGE );
Ti.UI.currentWindow.add( btnShareFB );
var btnShareTwitter = Ti.UI.createButton({
title: "Share to native Twitter app"
});
btnShareTwitter.addEventListener( "click", app.sharer.twitter.bind( null, MESSAGE ) );
Ti.UI.currentWindow.add( btnShareTwitter );
var btnShareChooser = Ti.UI.createButton({
title: "Share to Chooser"
});
btnShareChooser.addEventListener( "click", app.sharer.chooser.bind( null, MESSAGE ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment