Time for action – adding Twitter to your apps

We're going to implement Twitter in our apps by accessing a web service through UI buttons.

  1. In the Chapter 9 folder, copy the Twitter Web Pop-Up 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 the following variables at the beginning of the code:
    display.setStatusBar( display.HiddenStatusBar )
    
    local ui = require("ui")
    
    local openBtn
    local closeBtn
    local score = 100
  4. Create a local function called onOpenTouch() with an event parameter. Add an if statement so that the event receives a "release" action:
    local onOpenTouch = function( event )
      if event.phase == "release" then
  5. Using the local variable called message, add in the following string statement and concatenate score:
    local message = "Posting to Twitter from Corona SDK and got a final score of " ..score.. "."
  6. Add in local myString and apply string.gsub() for message to replace space instances:
    local myString = string.gsub(message, "( )", "%%20")
  7. Introduce the native.showWebPopup() function that links to the Twitter account. Concatenate myString to include the preloaded message. Close the function:
        native.showWebPopup(0, 0, 320, 300, "http://twitter.com/intent/tweet?text="..myString)
        
      end
    end
  8. Set up the openBtn UI function:
      openBtn = ui.newButton{
      defaultSrc = "openbtn.png",
      defaultX = 90,
      defaultY = 90,
      overSrc = "openbtn-over.png",
      overX = 90,
      overY = 90,
      onEvent = onOpenTouch,
    }
    
    openBtn.x = 110; openBtn.y = 350
  9. Create a local function called onCloseTouch() with an event parameter. Add an if statement with event.phase == "release" to activate native.cancelWebPopup():
    local onCloseTouch = function( event )
      if event.phase == "release" then    
      
        native.cancelWebPopup()    
      
      end
    end
  10. Set up the closeBtn UI function:
      closeBtn = ui.newButton{
      defaultSrc = "closebtn.png",
      defaultX = 90,
      defaultY = 90,
      overSrc = "closebtn-over.png",
      overX = 90,
      overY = 90,
      onEvent = onCloseTouch,
    }
    
    closeBtn.x = 210; closeBtn.y = 350
  11. Save the file and run the project in the simulator. Make sure you're connected to the Internet to see the results.
    Time for action – adding Twitter to your apps

Near the top of the code, we set a variable local score = 100. This will be used inour Twitter message.

In the onOpenTouch(event) function, a web popup will load on the release of openBtn. The text that will be posted is displayed in a string format under the variable, local message. You will notice that we concatenate score into the string so that it displays the value in the message post.

local myString and string.gsub() are used to replace all the instances indicated in a pattern inside the string. In this case, it takes the string inside a message and searches for every empty space between each word and replaces it with %20. %20 encodes URL parameters to indicate spaces. The extra % acts as an escape character.

The native.showWebPopup() function displays at dimensions 320 x 300, which is about half the screen size on a device. The URL to display the Twitter message dialog is added and concatenates myString.

When the web pop up no longer needs to be used and needs to be closed, onCloseTouch(event) is called by closeBtn. This will take the event parameter "release" and call native.cancelWebPopup(). This particular function will dismiss the current web popup.

Another social networking tool that can be used to share information about your game is Facebook. You can easily customize a post to link information about your game or share messages about high scores and to encourage other users to download it.

In order to post messages to Facebook, you need to be logged in to your Facebook account or create one at https://www.facebook.com/. You will have to obtain an App ID from the Facebook Developer website at https://developers.facebook.com/. The App ID is a unique identifier for your site that determines what the right level of security is in place between the user and the app page/website.

Once you have created an App ID, you will also need to edit the App information and choose how you want it to integrate with Facebook. You are given several choices, such as Website, Native iOS App, and Native Android App, just to name a few. The website integration must be selected and filled in with a valid URL in order for Facebook to redirect to the specified URL for posts that deal with web popups.