One of my favorite parts about Wax is how easy HTTP calls are, especially to REST APIs. 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 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" HTTPotluck.request{url, callback = yourCallback} end
That's it! You've got yourself an asynchronous HTTP call that parses a JSON 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.
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 should start with this article.
Since we are going to be talking to the Twitter's API we need to add the HTTPotluck and JSON extensions. It's easy though because they are bundled with Wax.
// The other imports are omitted from this sample code. #import "json.h" #import "HTTPotluck.h" @implementation TwitterAppAppDelegate @synthesize window; - (void)dealloc { [window release]; [super dealloc]; } - (void)applicationDidFinishLaunching:(UIApplication *)application { [window makeKeyAndVisible]; wax_startWithExtensions(luaopen_HTTPotluck, luaopen_json, nil); } @end
We are going to list the current Twitter trends in a UITableView. First create a new file called APP_ROOT/data/scripts/TwitterTableViewController.lua
Create a UITableViewController subclass with this call
waxClass{"TwitterTableViewController", UI.TableViewController}
Create the init function
function init(self) self.super:initWithStyle(UITableViewStylePlain) self.trends = {} return self end
Implement UIDataSource 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 UI.TableViewCell: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
Now we are going to use HTTPotluck to get info from the Twitter api. HTTPotluck 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 HTTPotluck.request{"http://search.twitter.com/trends.json", callback = trendsDownloaded} end
It's not terribly exciting, but that's the best part! Since Lua has scoped closures we can create the function trendsDownloaded and pass it into the HTTPotluck 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 HTTPotluck transforms the response body's JSON text into Lua table. You can check out what's inside a Lua table with a call like this puts(json).
You'll also need to update the tableView_cellForRowAtIndexPath and the init functions (check out the source)
Run it! You've got Twitter trends in a table view!
There are obvious improvements we can make but for now that seems like a good jumping off point for making HTTP requests with Wax!
Recent Posts