Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

9. Handling Multiple Devices and Network... > Time for action – adding Twitter in ...

Time for action – adding Twitter in your apps

Time for action – adding Twitter in your apps We're going to implement Twitter in our apps by accessing a web service through UI buttons. 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 accompanying this book from the Packt website.Create a new main.lua file and save it to the project folder.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 Create a local function called onOpenTouch() with an event parameter. Add in an if statement so the event receives a "release" action. local onOpenTouch = function( event ) if event.phase == "release" then 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.. "." Add in local myString and apply a string.gsub() for message to replace space instances. local myString = string.gsub(message, "( )", "%%20") Introduce the native.showWebPopup() function that links to the Twitter account. Concatenate myString to include the pre-loaded message. Close the function. native.showWebPopup(0, 0, 320, 300, "http://twitter.com/intent/tweet?text="..myString) end end 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 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 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 Save the file and run the project in the simulator. Make sure you're connected to the Internet to see the results. Note If you're currently not logged in to your Twitter account, you'll be asked to log in before you see the results of the tweet from our code. What just happened? Near the top of the code we set a variable, local score = 100. This will be used in our Twitter message. In the onOpenTouch(event) function, a web pop-up will load upon 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 it displays the value in the message post. local myString, string.gsub() is used to replace all the instances indicated in a pattern inside the string. In this case, it takes the string inside 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; 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 the native.cancelWebPopup(). This particular function will dismiss the current web pop-up. Posting to Facebook 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 insist upon other users downloading it. In order to post messages to Facebook, you need to be logged in to your Facebook account or create one at: http://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 dealing with web pop-ups.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint