Skip to content

Instantly share code, notes, and snippets.

@oroce
Created November 2, 2011 18:20
Show Gist options
  • Save oroce/1334424 to your computer and use it in GitHub Desktop.
Save oroce/1334424 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

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
Copy link
Author

oroce commented Jul 31, 2013

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

@ankesh27
Copy link

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
Copy link

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
Copy link

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
Copy link

@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
Copy link

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
Copy link

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