1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | -- -- Project: Facebook Connect sample app -- -- Date: December 24, 2010 -- -- Version: 1.3 -- -- File name: main.lua -- -- Author: Ansca Mobile -- -- Abstract: Presents the Facebook Connect login dialog, and then posts to the user's stream -- (Also demonstrates the use of external libraries.) -- -- Demonstrates: webPopup, network, Facebook library -- -- File dependencies: ui.lua, facebook.lua, json.lua libraries (included) -- -- Target devices: Simulator and Device -- -- Limitations: Requires internet access; no error checking if connection fails -- -- Update History: -- v1.1 Layout adapted for Android/iPad/iPhone4 -- v1.2 Modified for new Facebook Connect API (from build #243) -- v1.3 Added buttons to: Post Message, Post Photo, Show Dialog, Logout -- v1.4 Added ...{"publish_stream"} .. permissions setting to facebook.login() calls. -- -- Comments: -- Requires API key and application secret key from Facebook. To begin, log into your Facebook -- account and add the "Developer" application, from which you can create additional apps. -- -- Sample code is MIT licensed, see http://developer.anscamobile.com/code/license -- Copyright (C) 2010 ANSCA Inc. All Rights Reserved. -- --------------------------------------------------------------------------------------- -- Comment out the next line when through debugging your app. io.output():setvbuf('no') -- **debug: disable output buffering for Xcode Console **tjn -- Load external library (should be in the same folder as main.lua) local ui = require("ui") local facebook = require("facebook") local json = require("json") display.setStatusBar( display.HiddenStatusBar ) -- Facebook Commands local fbCommand -- forward reference local LOGOUT = 1 local SHOW_DIALOG = 2 local POST_MSG = 3 local POST_PHOTO = 4 local GET_USER_INFO = 5 local GET_PLATFORM_INFO = 6 -- Layout Locations local ButtonOrigY = 175 local ButtonYOffset = 45 local StatusMessageY = 420 -- position of status message local background = display.newImage( "facebook_bkg.png", true ) -- flag overrides large image downscaling background.x = display.contentWidth / 2 background.y = display.contentHeight / 2 -- This function is useful for debugging problems with using FB Connect's web api, -- e.g. you passed bad parameters to the web api and get a response table back local function printTable( t, label, level ) if label then print( label ) end level = level or 1 if t then for k,v in pairs( t ) do local prefix = "" for i=1,level do prefix = prefix .. "\t" end print( prefix .. "[" .. tostring(k) .. "] = " .. tostring(v) ) if type( v ) == "table" then print( prefix .. "{" ) printTable( v, nil, level + 1 ) print( prefix .. "}" ) end end end end local function createStatusMessage( message, x, y ) -- Show text, using default bold font of device (Helvetica on iPhone) local textObject = display.newText( message, 0, 0, native.systemFontBold, 24 ) textObject:setTextColor( 255,255,255 ) -- A trick to get text to be centered local group = display.newGroup() group.x = x group.y = y group:insert( textObject, true ) -- Insert rounded rect behind textObject local r = 10 local roundedRect = display.newRoundedRect( 0, 0, textObject.contentWidth + 2*r, textObject.contentHeight + 2*r, r ) roundedRect:setFillColor( 55, 55, 55, 190 ) group:insert( 1, roundedRect, true ) group.textObject = textObject return group end local statusMessage = createStatusMessage( " Not connected ", 0.5*display.contentWidth, StatusMessageY ) -- New Facebook Connection listener -- local function listener( event ) --- Debug Event parameters printout -------------------------------------------------- --- Prints Events received up to 20 characters. Prints "..." and total count if longer --- print( "Facebook Listener events:" ) local maxStr = 20 -- set maximum string length local endStr for k,v in pairs( event ) do if string.len(v) > maxStr then endStr = " ... #" .. tostring(string.len(v)) .. ")" else endStr = ")" end print( " " .. tostring( k ) .. "(" .. tostring( string.sub(v, 1, maxStr ) ) .. endStr ) end --- End of debug Event routine ------------------------------------------------------- print( "event.name", event.name ) -- "fbconnect" print( "event.type:", event.type ) -- type is either "session" or "request" or "dialog" print( "isError: " .. tostring( event.isError ) ) print( "didComplete: " .. tostring( event.didComplete) ) ----------------------------------------------------------------------------------------- -- 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" statusMessage.textObject.text = event.phase -- tjn Added print( "Session Status: " .. event.phase ) if event.phase ~= "login" then -- Exit if login error return end -- The following displays a Facebook dialog box for posting to your Facebook Wall if fbCommand == SHOW_DIALOG then facebook.showDialog( {action="stream.publish"} ) end -- Request the Platform information (FB information) if fbCommand == GET_PLATFORM_INFO then facebook.request( "platform" ) -- **tjn Displays info about Facebook platform end -- Request the current logged in user's info if fbCommand == GET_USER_INFO then facebook.request( "me" ) -- facebook.request( "me/friends" ) -- Alternate request end -- This code posts a photo image to your Facebook Wall -- if fbCommand == POST_PHOTO then local attachment = { name = "Developing a Facebook Connect app using the Corona SDK!", link = "http://developer.anscamobile.com/forum", caption = "Link caption", description = "Corona SDK for developing iOS and Android apps with the same code base.", picture = "http://developer.anscamobile.com/demo/Corona90x90.png", actions = json.encode( { { name = "Learn More", link = "http://anscamobile.com" } } ) } facebook.request( "me/feed", "POST", attachment ) -- posting the photo end -- This code posts a message to your Facebook Wall if fbCommand == POST_MSG then local time = os.date("*t") local postMsg = { message = "Posting from Corona SDK! " .. os.date("%A, %B %e") .. ", " .. time.hour .. ":" .. time.min .. "." .. time.sec } facebook.request( "me/feed", "POST", postMsg ) -- posting the message end ----------------------------------------------------------------------------------------- elseif ( "request" == event.type ) then -- event.response is a JSON object from the FB server local response = event.response if ( not event.isError ) then response = json.decode( event.response ) if fbCommand == GET_USER_INFO then statusMessage.textObject.text = response.name printTable( response, "User Info", 3 ) print( "name", response.name ) elseif fbCommand == POST_PHOTO then printTable( response, "photo", 3 ) statusMessage.textObject.text = "Photo Posted" elseif fbCommand == POST_MSG then printTable( response, "message", 3 ) statusMessage.textObject.text = "Message Posted" else -- Unknown command response print( "Unknown command response" ) statusMessage.textObject.text = "Unknown ?" end --[[ -- Display table of friends (not used at this time) ** Currently not used ** local friends = {} local data = response.data for i=1,#data do local name = data[i].name table.insert( friends, name ) end local topBoundary = display.screenOriginY + 40 local bottomBoundary = display.screenOriginY + 0 -- create the list of items myList = tableView.newList{ data=friends, default="listItemBg.png", --default="listItemBg_white.png", over="listItemBg_over.png", -- onRelease=listButtonRelease, top=topBoundary, bottom=bottomBoundary, } --]] else -- Post Failed statusMessage.textObject.text = "Post failed" printTable( event.response, "Post Failed Response", 3 ) end elseif ( "dialog" == event.type ) then -- showDialog response -- print( "dialog response:", event.response ) statusMessage.textObject.text = event.response end end --------------------------------------------------------------------------------------------------- -- NOTE: To create a mobile app that interacts with Facebook Connect, first log into Facebook -- and create a new Facebook application. That will give you the "API key" and "application secret" -- that should be used in the following lines: local appId = nil -- Add your App ID here local apiKey = nil -- Not needed at this time --------------------------------------------------------------------------------------------------- -- NOTE: You must provide a valid application id provided from Facebook if ( appId ) then -- *** -- ************************ Buttons Functions ******************************** -- *** local function postPhoto_onRelease( event ) -- call the login method of the FB session object, passing in a handler -- to be called upon successful login. fbCommand = POST_PHOTO facebook.login( appId, listener, {"publish_stream"} ) end local function getInfo_onRelease( event ) -- call the login method of the FB session object, passing in a handler -- to be called upon successful login. fbCommand = GET_USER_INFO facebook.login( appId, listener, {"publish_stream"} ) end local function postMsg_onRelease( event ) -- call the login method of the FB session object, passing in a handler -- to be called upon successful login. fbCommand = POST_MSG facebook.login( appId, listener, {"publish_stream"} ) end local function showDialog_onRelease( event ) -- call the login method of the FB session object, passing in a handler -- to be called upon successful login. fbCommand = SHOW_DIALOG facebook.login( appId, listener, {"publish_stream"} ) end local function logOut_onRelease( event ) -- call the login method of the FB session object, passing in a handler -- to be called upon successful login. fbCommand = LOGOUT facebook.logout() end -- *** -- ************************ Create Buttons ******************************** -- *** -- "Post Photo with Facebook" button local fbButton = ui.newButton{ default = "fbButton184.png", over = "fbButtonOver184.png", onRelease = postPhoto_onRelease, text = " Post Photo", x = 160, y = ButtonOrigY } -- "Post Message with Facebook" button local fbButton = ui.newButton{ default = "fbButton184.png", over = "fbButtonOver184.png", onRelease = postMsg_onRelease, text = "Post Msg", x = 160, y = ButtonOrigY + ButtonYOffset * 1 } -- "Show Dialog Info with Facebook" button local fbButton = ui.newButton{ default = "fbButton184.png", over = "fbButtonOver184.png", onRelease = showDialog_onRelease, text = " Show Dialog", x = 160, y = ButtonOrigY + ButtonYOffset * 2 } -- "Get User Info with Facebook" button local fbButton = ui.newButton{ default = "fbButton184.png", over = "fbButtonOver184.png", onRelease = getInfo_onRelease, text = "Get User", x = 160, y = ButtonOrigY + ButtonYOffset * 3 } -- "Logout with Facebook" button local fbButton = ui.newButton{ default = "fbButton184.png", over = "fbButtonOver184.png", onRelease = logOut_onRelease, text = "Logout", x = 160, y = ButtonOrigY + ButtonYOffset * 4 } else -- Handle the response from showAlert dialog boxbox -- local function onComplete( event ) if event.index == 1 then system.openURL( "http://developers.facebook.com/get_started.php" ) end end native.showAlert( "Error", "To develop for Facebook Connect, you need to get an API key and application secret. This is available from Facebook's website.", { "Learn More", "Cancel" }, onComplete ) end |