Skip to content
Corey Johnson edited this page Jan 13, 2011 · 1 revision

Making HTTP calls, especially to REST APIs, are one of my favorite parts about Wax. Basically, to make an HTTP request you just need this code.

    function getSomeTwitterTrends(self)
      local yourCallback = function(json, response)
        -- Do whatever you want here. It is called when the json response is received.
        -- This is where you can refresh a view, update a tableview, or whatevs.
      end

      local url = "http://search.twitter.com/trends.json"
      wax.http.get{url, callback = yourCallback}
    end

That's it! You've got yourself an asynchronous HTTP call that parses a JSON or XML response into a Lua Table!

Below I will step you through creating a simple Twitter app that lists the recent trends in a UITableView. You can get the source here.

Setup Wax

Open up Xcode and create a new project using the Wax template. You can name it whatever you want, I'm going to call mine TwitterApp. If you haven't setup Wax yet you check [this page|Installation] out first.

Let's write some Lua!

  1. We are going to list the current Twitter trends in a UITableView. First create a new file called APP_ROOT/scripts/TwitterTableViewController.lua

  2. Create a UITableViewController subclass with this call

  waxClass{"TwitterTableViewController", UITableViewController}
  1. Create the init function
    function init(self)
      self.super:initWithStyle(UITableViewStylePlain)
      self.trends = {}

      return self
    end
  1. Implement UITableViewDataSource as shown below, then run the app! you should have a real simple(boring) UITableView up and running!
    function numberOfSectionsInTableView(self, tableView)
      return 1
    end

    function tableView_numberOfRowsInSection(self, tableView, section)
      return #self.trends
    end

    function tableView_cellForRowAtIndexPath(self, tableView, indexPath)
      local identifier = "TwitterTableViewControllerCell"
      local cell = tableView:dequeueReusableCellWithIdentifier(identifier) or
                   UITableViewCell:initWithStyle_reuseIdentifier(UITableViewCellStyleDefault, identifier)

      local object = self.trends[indexPath:row() + 1] -- Must +1 because lua arrays are 1 based
      cell:textLabel():setText(object)

      return cell
    end

Twitter API

Now we are going to use wax.http to get info from the Twitter api. wax.http is a wrapper around the iPhone SDK's NSURLConnection class. Lua makes the entire processes SO MUCH easier. Here is the code we need to add to our TwitterTableViewController

    function viewDidLoad(self)
      local trendsDownloaded = function(json, response)
        if response:statusCode() == 200 then
          self.trends = json["trends"]
        else
          -- Ignore errors for now...
        end
        self:tableView():reloadData()
      end

      wax.http.get{"http://search.twitter.com/trends.json", callback = trendsDownloaded}
    end

It's not terribly exciting, but that's the best part! Since Lua has closures/blocks we can create the function trendsDownloaded and pass it into the wax.http request. When the request is finished trendsDownloaded is called with the json results (as a Lua table) and the NSHTTPURLResponse arguments. All thats left is to fill up our trends variable with the returned results!

Since Twitter returns Content-type=application/json wax.http transforms the response body's JSON text into Lua table, if it was application/xml it would parse that as well. You can check out what's inside a Lua table with a call like this puts(json).

  1. You'll also need to update the tableView_cellForRowAtIndexPath and the init functions (check out the source)

  2. Run it! You've got Twitter trends in a table view!

Improvements

There are obvious improvements we can make but for now that seems like a good jumping off point for making HTTP requests with Wax!

Clone this wiki locally