Web Development Blog

by Ei Sabai Nyo

25 Jun, 2011

How to ‘Post on Facebook Wall’ on iPhone and Android using PhoneGap plugins    

For iPhone / iPad (iOS)

Prerequsites
- Install ChildBrowser Phonegap plugin
- Copy FBConnectExample/FBConnect.js into your www folder and include it in your HTML file
- Read and follow the instructions from How to post to the Facebook wall in an iPhone app with phonegap, FaceBook Connect and ChildBrother plugin by MosaCrea

The Javascript(jQuery) code:

$('#share-facebook').live("touchstart touchend", function() {
	var url = $(this).attr("rel");
	try {
		shareonfacebook(" likes Web Development Blog by Ei Sabai.", "http://eisabainyo.net/weblog/wp-content/uploads/2010/11/profile-small.gif", url);
	} 
	catch(err) {
		console.log(err);
	}	
	return false;
});
function shareonfacebook(text, image, url) {
	var client_id = "xxxxxxxxxxxxx"; // Your Facebook Client ID
	var redir_url = "http://www.facebook.com/connect/login_success.html";

	if (typeof fbPlugin === 'undefined') {
		fbPlugin = FBConnect.install();
	}
	fbPlugin.connect(client_id, redir_url, "touch");
    fbPlugin.onConnect = function() {
	    window.plugins.fbConnect.postFBWall(text, url, image, function() {
			navigator.notification.alert(
				'Thank you for sharing on Facebook.',  
				function() {},         
				'Thank you',     
				'OK'                 
			);
		});
	};
}

For Android

Prerequsites
- Install Facebook for Android Phonegap plugin
- Copy facebook.js into your www folder and include it in your HTML file

The Javascript(jQuery) code:

$('#share-facebook').live("touchstart touchend", function() {
	var url = $(this).attr("rel");
	try {
		shareonfacebook(" likes Web Development Blog by Ei Sabai.", "http://eisabainyo.net/weblog/wp-content/uploads/2010/11/profile-small.gif", url);
	} 
	catch(err) {
		console.log(err);
	}	
	return false;
});
function postonfacebook(text, image, url, token) {
	$.ajax({
		   type: 'POST',
		   url: "https://graph.facebook.com/me/feed",
		   data: {
				message: text,
				picture: image,
				link: url,
				access_token: token,
				format: "json"
		   },
		   success: function (data) {
			   navigator.notification.alert(
					'Thank you for sharing on Facebook.',  
					function() {},         
					'Thank you',     
					'OK'                 
				);
		   },
		   dataType: "json",
		   timeout: 10000
	});
}
function shareonfacebook(text, image, url) {
	var client_id = "xxxxxxxxxxxxx"; // Your Facebook Client ID

	window.plugins.facebook.authorize(client_id, function(res){
		if (res.token !== undefined) {
			postonfacebook(text, image, url, res.token)
		} else {
			// call authorization method
			window.plugins.facebook.getAccess(function(res) {
				if (res.token !== undefined) {
					postonfacebook(text, image, url, res.token)
				}
			});
		}
	});
}

Usage (for both iPhone and Android):

<a href="#" rel="http://eisabainyo.net/weblog/" id="share-facebook">Share on Facebook</a>
Love what you've just read? Subscribe to our newsletter to receive tips, resources and special offers related to web development & design.
Your name:   Your email:  

One Response to “How to ‘Post on Facebook Wall’ on iPhone and Android using PhoneGap plugins”

  1. mork says:

    In Android emulator
    I got “generic error occurred” in popoup win after launch the FB plugin. Then if i press OK in dialog it display the logged user FB home page and nothing happens further inside popped win.
    Note: I use both a client id and an app id to configure authorize command.
    Also i’ve genereted the hash key for the debug view but error i still triggered.

    Using a workaround with childbrowser (launch a “proxy” page on remote server that do the job) is the only solution.

    Mork

Profile PicHello! Welcome to Web development blog! My name is Ei Sabai and on this blog, I write about web development, mobile app development, latest web technologies and the likes. Read more about me or have a look at some of the tips & resources I've written.
Subscribe to our newsletter to receive tips, resources and special offers related to web development & design.
We do NOT spam.
Your name:  
Your email:  

Tips & Resources

Tips & Resources
WordPress Web Hosting
Recommended web hosting providers for WordPress 3.0
iPhone Native App Development
Important steps into iPhone app development for beginners
iPhone Web App Development
Tips for iPhone web app development
Coupons for Web Developers
Get discounts on web hosting, domain names, templates, etc
10 Useful jQuery Snippets
Easy-to-use jQuery snippets for any website
HTML Email Newsletter
Step-by-step tutorial on how to code an HTML email newsletter
  • bluehost Hosting $6.95/month
  • Joomla Templates

Recommended Book

Categories