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 ) );
@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