» Setting up iPhone Wax

iPhone Wax is is a Lua framework to make iPhone app development faster! What is the advantage of writing apps in Lua? Lua is a modern dynamic language, like Ruby or Python. Because of that you can be more expressive in less lines of code, take advantage of modern language techniques and extend Lua any way you see fit!

Install Wax

  1. Go to http://github.com/probablycorey/wax and do what the README says.

  2. Don't know Lua? If you are familiar with Javascript, Python, Ruby or any dynamic language you can learn the basics of Lua in 15 minutes! You can check out these free tutorials or buy a copy of Programing in Lua.

Create a new project

  1. Open up Xcode and create a new project using the Wax template (The wax template is installed by following these instructions). You can name the project whatever you want, I'm going to be super generic and call mine TableView.

  2. From now on APP_ROOT references the path of this Xcode project.

  3. Run the app! You'll get a simple "Hello Lua" program running in your iPhone Simulator!

Let's write some Lua!

  1. You don't need to edit the Lua code from Xcode, it's all located in the APP_ROOT/date/scripts directory, so open up your favorite text editor and go at it! You should also look at Wax's support files at APP_ROOT/wax/lib/wax-scripts.

    • If you want to use TextMate type rake tm from APP_ROOT to setup a project. Also install the wax-bundle for some helpful keyboard shortcuts.
    • If Xcode strikes your fancy, you could try capgo.com's syntax highlighting plugin.
  2. To create the TableView, first create a new file called APP_ROOT/data/scripts/MyTableViewController.lua

  3. Create a new obj-c controller class with this call

    waxClass{"MyTableViewController", UITableViewController}
    

  4. Now add the init function, it will setup the tableView's contents and call the super's init (just like in obj-c!)

    function init(self)
      self.super:initWithStyle(UITableViewStylePlain)
      
       -- Here are the tableView's contents
      self.things = {"Planes", "Trains", "Automobiles"}
    
      return self
    end
    

  5. Now we need to implement methods from the UIDataSource protocol. The final MyTableViewController.lua file should look like this.

    waxClass{"MyTableViewController", UITableViewController}
        
    function init(self)
      self.super:initWithStyle(UITableViewStylePlain)
    
       -- Here are the tableView's contents
      self.things = {"Planes", "Trains", "Automobiles"}
    
      return self
    end
        
    function numberOfSectionsInTableView(self, tableView)
      return 1
    end
    
    function tableView_numberOfRowsInSection(self, tableView, section)
      return #self.things
    end
    
    function tableView_cellForRowAtIndexPath(self, tableView, indexPath)
      local identifier = "MyTableViewControllerCell"
      local cell = tableView:dequeueReusableCellWithIdentifier(identifier) or
                   UITableViewCell:initWithStyle_reuseIdentifier(UITableViewCellStyleDefault, identifier)
    
      local thing = self.things[indexPath:row() + 1] -- Must +1 because Lua arrays are 1 based
      cell:textLabel():setText(thing)
    
      return cell
    end
    

  6. All that is left is to create an instance of MyTableViewController and add it to the main window. We do this by editing APP_ROOT/data/scripts/AppDelegate.lua. This is the same as the UIApplicationDelegate you use in an Objective-C app.

    require "MyTableViewController"
    
    waxClass{"AppDelegate", protocols = {"UIApplicationDelegate"}}
    
    function applicationDidFinishLaunching(self, application)
      local frame = UIScreen:mainScreen():bounds()
      self.window = UIWindow:initWithFrame(frame)
      
      self.controller = MyTableViewController:init()
      self.window:addSubview(self.controller:view())
      
      self.window:makeKeyAndVisible()
    end
    

  7. Run the app... You will have a really simple/boring UITableView that was created via Lua!

Learn More

That's just the beginning of what you can do with iPhone Wax!

User Comments

Recent Posts

  • Interactive console for iOS! - August 20, 2010
  • Archive

    • Letter to Steve Jobs - April 11, 2010
    • Wax talks to Twitter - October 20, 2009
    • How does iPhone Wax work? - October 19, 2009
    • Setting up iPhone Wax - October 18, 2009
    • Ruby (tinyrb) on iPhone - May 03, 2009
    • Building PCRE static lib for the iPhone - May 02, 2009
    • Amazon EC2 + Chef = Mmmmm - March 29, 2009
    • Objective-c key paths - February 13, 2009
    • POW! - December 26, 2008
    • Abusing Ruby's question mark methods. - November 28, 2008
    • Git hooks make me giddy - November 07, 2008
    • Ruby Equality! equal? eql? == and === - October 26, 2008
    • Ruby, Rails and Google Sitemaps - October 20, 2008
    • Projects

      • Wax Obj-C to Lua bridge for iPhone.
      • Pow a Ruby library for making file & directory manipulation easy.
      • MiniMagick a tiny RMagick replacement.