Time for action – posting scores using Facebook Connect

Facebook Connect is another way to post on the wall feed by using the native Facebook UI features. We'll be creating a different way to post messages and scores to the newsfeed. In order to see how Facebook Connect operates, you need to load the build to a device to view the results. It will not run in the simulator.

  1. In the Chapter 9 folder, copy the Facebook Connect project folder to your desktop. All the configuration, libraries, and assets needed are already included. You can download the project files that accompany this book from the Packt Publishing website.
  2. Create a new main.lua file and save it to the project folder.
  3. Set up the following variables at the beginning of the code:
    display.setStatusBar( display.HiddenStatusBar )
    
    local ui = require("ui")
    local facebook = require "facebook"
    
    local fbBtn
    local score = 100
  4. Create a local function called onFBTouch() with an event parameter. Add an if statement that contains event.phase == release. Also, include your Facebook app ID in a string format:
    local onFBTouch = function( event )
      if event.phase == "release" then    
        
        local fbAppID = "0123456789" -- Your FB App ID from facebook developer's panel
    
  5. Create another local function within onFBTouch(event) called facebookListener() with an event parameter as well. Include an if statement that refers to "session" == event.type:
        local facebookListener = function( event )
          if ( "session" == event.type ) then
  6. Add in another if statement where "login" equals to event.phase. Include a local variable called theMessage to display the message you want to share with other Facebook users:
            if ( "login" == event.phase ) then  
            
              local theMessage = "Got a score of " .. score .. " on Your App Name Here!"  
  7. Add the facebook.request() function that will post the following messages to the user's Facebook wall. Close any remaining if statements with end in the facebookListener(event) function:
              facebook.request( "me/feed", "POST", {
                message=theMessage,
                name="Your App Name Here",
                caption="Download and compete with me!",
                link="http://itunes.apple.com/us/app/your-app-name/id382456881?mt=8",
                picture="http://www.yoursite.com/yourimage.png"} )
            end
          end
        end
  8. Call the facebook.login() function that includes your App ID, listener, and permissions to post on a user's Facebook wall. Close the remainder of the onFBTouch(event) function:
        facebook.login(fbAppID, facebookListener, {"publish_actions"})
        
      end
    end
  9. Enable the fbBtn UI function and save your file:
    fbBtn = ui.newButton{
      defaultSrc = "facebookbtn.png",
      defaultX = 100,
      defaultY = 100,
      overSrc = "facebookbtn-over.png",
      overX = 100,
      overY = 100,
      onEvent = onFBTouch,
    }
    
    fbBtn.x = 160; fbBtn.y = 160
  10. Create a new device build for either iOS or Android. Load the build to your device and run the application. You will be asked to log in to your Facebook account before you can see the results from the application.
    Time for action – posting scores using Facebook Connect

One of the most important things that need to be done is require "facebook" in order to have the Facebook API to work. We also created a local variable called score with the value of 100.

The onFBTouch(event) function will initiate the event parameter on "release" of fbBtn. Within the function, fbAppID is included with characters in a string format. This will be a unique set of numbers that you must obtain from the Facebook Developers website. The App ID will be created for you when you make an App page on the site.

Another function, facebookListener(event), is created, and it will initiate all fbConnect events. The if statement that contains ("login" == event.phase ) will request to post a message to your feed through "me/feed, "POST". The feed contains the following:

After the parameters are set, facebook.login() will refer to fbAppID and facebookListener() to see if a valid application ID is being used to post on Facebook. On success, the post is published through "publish_actions".

See if you can figure out how to display a dialog box using Facebook Connect and using the same setup as shown in the preceding example. The following line will display this as:

Now, see where in the code facebook.showDialog() can be accessed. This is another way of posting messages to Facebook.

Q1. What is the specific API that scales down high-resolution sprite sheets?

Q2. What are the publishing permissions called that allow posting on a user's wall on Facebook?

Q3. Which parameter(s) is required for facebook.login()?