Hi Tom,
I don’t really have time at the moment to put together a full working demonstration that is not our complete project, but here are a few larger snipets… Right now, I am away from my mac, so I cannot build this for device. and test
facebookUI.lua
local facebook = require("facebook")
local json = require("json")
local factory = {}
-- Facebook Session States
local fbPhases = {
login = "Logged In",
loginFailed = "Login Failed",
loginCancelled = "Login Cancelled",
logout = "Logged Out",
}
-- Facebook Connection listener
--
local function listener( request, event )
-- print("facebookUI.listener invoked", dialog, request, event)
-- After a successful login event, send the FB command
-- Note: If the app is already logged in, we will still get a "login" phase
--
if ( "session" == event.type ) then
-- event.phase is one of: "login", "loginFailed", "loginCancelled", "logout"
print( "facebookUI listener, Session Status: " .. event.phase, request )
factory.sessionState = event.phase
end
if ( "session" == event.type ) then
if "login" == event.phase then
print("Login complete, invoking facebook.request", request.path)
if request.action then
facebook.showDialog( request.action, request.attachment )
else
facebook.request( request.path, request.method, request.attachment ) -- posting the photo
end
else
print("Login failed: ", event.phase)
end
elseif ( "request" == event.type ) or ( "dialog" == event.type ) then
-- event.response is a JSON object from the FB server
local response = event.response
if event.isError then
print("Facebook connect error:", json.encode(event))
else
if "dialog" == event.type and (not(response and string.find(response, "post\_id="))) then
print("Dialog cancelled.")
if request.onError then
request.onError("Canceled")
end
else
print("Facebook Response:",response)
local decodedResponse = json.decode(response) or response
if decodedResponse and decodedResponse.error then
print("Facebook refused request.\n" .. (decodedResponse.error.message or "") )
else
if request.onResponse then
-- invoke callback with decoded json, or plan response if not json encoded
request.onResponse(request, decodedResponse)
end
end
end
end
else
print("facebookUI listener - unhandeld event:", event.type)
end
end
function factory.sendMessage( request )
-- call the login method of the FB session object, passing in a handler
-- to be called upon successful login.
-- wrap callback to provide context to listener
print("invoking facebook.login for ", request.path, request)
facebook.login( game.facebookAppId,
function(event)
listener(request, event)
end,
request.permissions
--{"publish\_stream","user\_photos"}
)
end
other module:
local fbUI = require( "facebookUI" )
local function getFriendsListener(request, response)
local friendsList = response and response.data
local contextData = request.contextData
-- process the results in friendsList
end
local function getFriends()
local request = {
path = "me/friends",
attachment = {
fields="installed,name,picture.height(90).width(90)"
},
onResponse = getFriendsListener,
onError = function(msg) print("failed", msg) end,
contextData = "Other info",
}
fbUI.sendMessage(request)
end
The request table above is just one example, it combines the data to send to facebook with success and error handlers, and can pass additional parameters to the onResponse listener.
It is getting late, I’ve got to go. [import]uid: 120928 topic_id: 34416 reply_id: 142711[/import]
