Some things with Facebook are easy while others are more challenging. Getting a list of Facebook friends has been, traditionally, one of the more challenging tasks: you need to use facebook.request() to ask for the
"me/friends"
object, convert the huge JSON object into a Lua table, and then load that into a newTableView() widget — including downloading each friend’s avatar and inserting it. Then you need to handle the potential selection of several friends and work the data back into your app where you can use it.
Fortunately, Corona SDK offers an easier way to select a set of Facebook friends using the facebook.showDialog() API.
Initial Setup
With any app that uses Facebook, you have to set it up so it will communicate with Facebook. This, in itself, is not a trivial process — see the Implementing Facebook Guide for details.
On iOS, you must include (at the minimum) the following in your build.settings
plist
section:
1 2 3 4 5 6 |
FacebookAppID = "XXXXXXXXXX", -- Replace XXXXXXXXXX with your Facebook App ID CFBundleURLTypes = -- This set of braces is important! CFBundleURLSchemes = "fbXXXXXXXXXX", } }, |
In addition, make sure that you build with a provisioning profile that matches the com.yoursite.yourapp
entry which you added on the Facebook Developer Portal.
On Android, you must build your app with the Keystore and Alias of the keystore which you used to build the keyhash from the developer portal.
App Code
Next, you should set up the basic Facebook login processor. This includes a require
of the Facebook library, a listener to handle the login process, and logging in to Facebook.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
local facebook = require( "facebook" ) -- 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 -- Show the friends picker facebook.showDialog( "friends", onComplete ) end elseif ( "dialog" == event.type ) then print( event.response ) end end local FBAppID = "XXXXXXXXXXX" -- Facebook App ID from build.settings, without the "fb" prefix. facebook.login( FBAppID, listener, "publish_actions" ) |
Now the Facebook system is initialized. As for the “Friends” dialog itself, it requires a callback listener which gathers the list of friends returned:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
local function onComplete( event ) print( "event.name:", event.name ) print( "event.type:", event.type ) if ( event.data ) then -- event.data is a table of friends returned. -- event.data[1] will be the first friend returned. -- Each friend will have a list of fields returned like: ----- event.data[1].firstName ----- event.data[1].fullName ----- event.data[1].lastName ----- event.data[1].id end end |
Next, set up a “show dialog” function and a basic button to trigger it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
local widget = require( "widget" ) local function showDialog( event ) if ( event.phase == "ended" ) then facebook.showDialog( "friends", onComplete ) end return true end local button = widget.newButton label = "Pick Friends", onEvent = showDialog, button.x = display.contentCenterX button.y = display.contentCenterY |
In Summary
With this code in place, you can easily pick a list of friends to social network with. What you do from there is up to your imagination!
Excerpt from:
Tutorial: Getting Facebook Friends (the Easy Way)
Comments by SDK News
New partnership to bring Android to the open road
Hello Lisha, You can visit openautoalliance.net and stay ...
Windows 8 to Windows 8.1 Preview starting with the XAML templates
Hello David, Yes, if you upgrade your app to 8.1, then ...