Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Appcelerator share to native facebook, twitter
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 ) );
@samih-harfouche

This comment has been minimized.

Copy link

@samih-harfouche samih-harfouche commented Apr 25, 2013

i am getting the below error any idea what might be the problem?
"unable to find explicit activity class {com.facebook.katana/com.facebook.katana.ShareLinkActivity}; have you declared this activity in your AndroidManifest.xml?"

i am usind using sdk 3.1
android api 13

it works well for twitter though

I found the solution:
var intFB = Ti.Android.createIntent({
action : Ti.Android.ACTION_SEND,
packageName : "com.facebook.katana",
type : "text/plain"
});
intFB.putExtra(Ti.Android.EXTRA_TEXT, "http://www.google.com");
Ti.Android.currentActivity.startActivity(intFB);

@dottodot

This comment has been minimized.

Copy link

@dottodot dottodot commented Jul 3, 2013

I'm really struggling to get this working for Facebook, Twitter works perfectly but Facebook always opens the browser version. Any suggestions how I might find what the problem is?

@oroce

This comment has been minimized.

Copy link
Owner Author

@oroce oroce commented Jul 31, 2013

it seems, recently you dont need to specify the className: http://stackoverflow.com/a/14951631/904525

@ankesh27

This comment has been minimized.

Copy link

@ankesh27 ankesh27 commented May 22, 2014

In chooser I see all the options that can share TEXT, now when I choose Facebook I do not see any text pre filled.

Also, what if I want to share a URL on Facebook ?

Any idea on the same ?

@Ganeshkumarcaro

This comment has been minimized.

Copy link

@Ganeshkumarcaro Ganeshkumarcaro commented Sep 12, 2014

i found the solution..for posting image in twitter..

var postrTwitter = 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"
});
postrTwitter.setType("/");
postrTwitter.putExtra(Ti.Android.EXTRA_TEXT, "Text message ");
postrTwitter.putExtraUri(Ti.Android.EXTRA_STREAM, image_file.nativePath);

        Ti.Android.currentActivity.startActivity(postrTwitter);
@Ganeshkumarcaro

This comment has been minimized.

Copy link

@Ganeshkumarcaro Ganeshkumarcaro commented Sep 12, 2014

While Posting in facebook. the text and image is not adding. i can get only the empty post screen ..

var intFB = Ti.Android.createIntent({
action : Ti.Android.ACTION_SEND,
packageName : "com.facebook.katana",
type : "text/plain"
});

intFB.putExtra(Ti.Android.EXTRA_TEXT, "http://www.google.com");
intFB.putExtraUri(Ti.Android.EXTRA_STREAM, image_file.nativePath);
Ti.Android.currentActivity.startActivity(intFB);

Wat is the mistake in this..? please explain..

@justingreerbbi

This comment has been minimized.

Copy link

@justingreerbbi justingreerbbi commented Oct 8, 2014

@Ganeshkumarcaro
Change type to "image/png" and not "text/plain". FB does not allow you to set any text so this is why you are seeing the not text. You are not seeing any image because you are using the wrong type.. ;)

@mamunabcoder

This comment has been minimized.

Copy link

@mamunabcoder mamunabcoder commented May 17, 2015

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.twitter.android/com.twitter.android.PostActivity}; have you declared this activity in your AndroidManifest.xml?

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.setType("/");
intTwitter.putExtra(Ti.Android.EXTRA_TEXT, social_message + "\n" + User.promoter_code);
//intTwitter.putExtraUri(Ti.Android.EXTRA_STREAM, image_file.nativePath);
//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
}

This code is not working. how to solved it? how to put the package name and class name in android manifest xml?

@chmiiller

This comment has been minimized.

Copy link

@chmiiller chmiiller commented Jan 19, 2017

I simply removed the class name and it worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.