Following our teaser from earlier today, we wanted to give you a preview/glimpse into how easy it soon will be to integrate Facebook into your app.

Behind the scenes, we’re integrating Facebook’s OAuth-based library for FBConnect support. It’s going to be insanely easy to access Facebook’s graph API.

Here’s the breakdown:

It’s a one-liner to prompt the user for login. The first argument is the app id that you get from Facebook and the second is a listener that responds to “fbconnect” events:
[cc lang=”lua”]
facebook.login( “1234567890”, listener )[/cc]

Once the user has logged in, it’s one line to get that user’s friends list:
[cc lang=”lua”]
facebook.request( “me/friends” )[/cc]

And one more line to convert the JSON response into a Lua table:
[cc lang=”lua”]
response = json.decode( event.response )[/cc]

Most of the rest of the code is for listener management and creating the scrolling list of friends — I’ve blurred the names to protect the innocent. (BTW, the scrolling list is part of our cross-device UI framework. There’s a quick tutorial here).

Here’s the entire code:

[cc lang=”lua”]
local facebook = require “facebook”
local json = require “json”
local tableView = require(“tableView”)

— listener for “fbconnect” events
local function listener( event )
if ( “session” == event.type ) then
— upon successful login, request list of friends
if ( “login” == event.phase ) then
facebook.request( “me/friends” )
elseif ( “request” == event.type ) then
— event.response is a JSON object from the FB server
local response = event.response

— if request succeeds, create a scrolling list of friend names
if ( not event.isError ) then
response = json.decode( event.response )

local friends = {}
local data =
for i=1,#data do
local name = data[i].name
table.insert( friends, name )

local topBoundary = display.screenOriginY + 40
local bottomBoundary = display.screenOriginY + 0

— create the list of items
myList = tableView.newList{
default=”listItemBg.png”, over=”listItemBg_over.png”,
top=topBoundary, bottom=bottomBoundary,
elseif ( “dialog” == event.type ) then
print( “dialog”, event.response )

— first argument is the app id that you get from Facebook
facebook.login( “1234567890”, listener )

Share this post....Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0
  1. Wow I was looking for this exact thing a while back, and ended up creating my own app-server-app code, but it requires an external web server.

    Thanks for this advice! In the future it’ll allow me to make graph requests straight from the app without the need for an external web server.

  2. This is what I love about having switched over from Titanium to Corona – much more openness on what’s coming up, simpler APIs – which actually work – and what so far seems like a much faster pace of development. Keep up the good work!

    Looking forward to getting this up and running soon. Maps API would be nice too 😉

  3. Please provide support for the Android platform. Release for iOS was over 3 months ago. This makes little sense considering one of Corona’s selling points is to simplify cross-platform development. I will not be investing in your software until this and similar problems are corrected.

  4. Great example but do you know how I can pull the name out of the list for use? I’ve tried data[id].name and a few others but I can’t get the listener to recognize what I’m clicking on in order to display it even in a simple alert.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>