-- ----------------------------------------------------------------------------------------- -- -- Facebook Button Configuration -- ----------------------------------------------------------------------------------------- -- Facebook listener local function facebookListener( event ) if "request" == event.type then if event.isError then native.showAlert( "Request Error", "Error trying to get the current user", { "OK" } ) return end local response = json.decode( event.response ) if response then -- Store Users First Name myData.firstName = response.first\_name local firstName\_path = system.pathForFile( "firstName.txt", system.DocumentsDirectory ) local fh\_firstName = io.open( firstName\_path, "w+" ) -- save the userName if fh\_firstName then fh\_firstName:write( myData.firstName ) io.close( fh\_firstName ) fh\_firstName = nil end myData.lastName = response.last\_name local lastName\_path = system.pathForFile( "lastName.txt", system.DocumentsDirectory ) local fh\_lastName = io.open( lastName\_path, "w+" ) -- save the userName if fh\_lastName then fh\_lastName:write( myData.lastName ) io.close( fh\_lastName ) fh\_lastName = nil end myData.userID = response.id local userID\_path = system.pathForFile( "userID.txt", system.DocumentsDirectory ) local fh\_userID = io.open( userID\_path, "w+" ) -- save the userName if fh\_userID then fh\_userID:write( myData.userID ) io.close( fh\_userID ) fh\_userID = nil end end local function networkListener( event ) if event.isError then native.showAlert( "Network Error", "Download of profile picture failed, please check your network connection", { "OK" } ) else end fbBtn.isVisible = false -- Reload the menu composer.gotoScene( "restartStart", "fade", 500 ) end -- Download the profile picture local path = system.pathForFile( myData.firstName .. myData.lastName .. myData.userID .. ".png", system.TemporaryDirectory ) local picDownloaded = io.open( path ) if not picDownloaded then network.download( "http://graph.facebook.com/" .. myData.userID .. "/picture", "GET", networkListener, myData.firstName .. myData.lastName .. myData.userID .. ".png", system.TemporaryDirectory ) else fbBtn.isVisible = true -- May need something here -- like to reload the menu -- Reload the menu composer.gotoScene( "restartStart" ) end -- After a successful login event, send the FB command -- Note: If the app is already logged in, we will still get a "login" phase elseif "session" == event.type then -- event.phase is one of: "login", "loginFailed", "loginCancelled", "logout" if event.phase ~= "login" then -- Exit if login error return end -- Request the current logged in user's info if fbCommand == GET\_USER\_INFO then facebook.request( "me" ) end end return true end -- Login function local function onFbBtnRelease( event ) --fbBtn.isVisible = false if(event.phase == "began") then self.hasFocus = true -- sfx.play( sfx.menuPress, { channel = 2 } ) elseif(self.hasFocus) then if(event.phase == "cancelled") then self.hasFocus = false return false elseif(event.phase == "ended") then self.hasFocus = false fbBtn.isVisible = false if ( appId ) then event.target.isVisible = false -- 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, facebookListener, {"publish\_actions"} ) else local function onComplete( event ) system.openURL( "http://developers.facebook.com/setup" ) end native.showAlert( "Error", "To develop for Facebook Connect, you need to get an application id from Facebook's website.", { "Learn More" }, onComplete ) end menuButtons() end end if (firstN) then firstN.isVisible = true end --logoutButton.isVisible = true return true end -- create a widget button fbBtn = widget.newButton{ defaultFile="fbLogo.png", overFile = "fbLogoOver.png", width=154, height=77, onEvent = onFbBtnRelease -- event listener function } fbBtn.x = \_W\*0.5 fbBtn.y = \_H\*0.8 fbBtn.isVisible = false
– Build Settings Part
plugins =
{
[“facebook”] =
{
publisherId = “com.coronalabs”,
supportedPlatforms = { iphone = true, [“iphone-sim”]=true },
},
},
-------------- THIS IS MY CODE, PLEASE HELP