m.stefankendall.com – My iPhone web apps

May 22, 2010

Recently, I’ve become very interested in iPhone web app development and Grails, so I decided to merge the two into a little project. Using Grails, jQTouch, and jQuery, I built an application to search a variety of torrent portals and aggregate the results. I could describe the application, but why not just head over to m.stefankendall.com and check it out?

If you don’t have an iPhone or Android-like device, here are some screenshots to show you what the interface looks like on a mobile device:

iPhone Web App Screenshot 1 iPhone Web App Screenshot 2 iPhone Web App Screenshot 3

Working with this project, I discovered a few interesting bits of information.

  1. jQTouch is incomplete
    I had to work through the source code of the CSS files and demo page to make any headway into this library. There is very little documentation, and some of the pre-built “apple” styles don’t look truly native. This is sad, but it does do some things pretty well. For instance, if you bind $.tap event to a jQuery object, and the target browser doesn’t support the $.tap javascript event, the event is bound to $.click instead. This is great for testing. I also found that while Safari for Windows is a close approximation for the iPhone browser, it’s not the same.

    For instance, if your page has any HTML errors, such as improperly closed <input /> tag, Safari for Windows won’t complain. Safari for the iPhone, however, will break all to hell and respond haphazardly to javascript events. To prevent this from happening to you, I highly recommend enabling Safari developer mode. See the screenshots below.

    How to enable Safari Developer mode How to enable Safari Developer mode

  2. Groovy has built-in JSON conversion support
    At first, I thought I was going to need to use something like GSON to automatically convert my objects into JSON, but Groovy can do this with built-in libraries. Check this out:

    		def namedResults = [:]
    
    		results.each { ProviderService key, value ->
    			namedResults[key.getName()] = value
    		}
    
    		render namedResults as JSON
    

    This produces

    {"Demonoid":["Result1", "Result2",...],...}

    which is trivially parseable via the javascript JSON parser at JSON.org.

  3. GPath is a decent substitute for XPath
    I’m generally a big fan of XPath, but GPath ain’t so bad. Specifically, trying to find all the deepest nodes which match a certain criterion is pretty easy.

    		def slurper = new XmlSlurper()
    		slurper.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
    
    		def doc = slurper.parseText(xml)
    
    		//Find anchor tags for torrent links
    		def allAnchorTags = doc.depthFirst().findAll { GPathResult node ->
    			node["@class"] == "detLink"
    		}
    

    By combining depthFirst() and findAll {…}, one can filter all nodes in an XML document succinctly and with ease. The equivalent XPath //a[@class='detLink'] would require some extra lifting to parse the results so neatly.

  4. Auto-wiring is magical
    In the course of building the TorrentSearch application, I used nothing but auto-wiring to configure my services and controllers. This probably doesn’t scale all that well, but it worked well enough for my purposes. I tend to use the path of least resistance until it no longer works, and auto-injection via field name is definitely the path of least resistance. Code snippet:

    class TorrentSearchController
    {
    	def torrentProviderService
               ...
    }
    

    Simply by defining the service, it gets auto-injected with a singleton instance of the class named TorrentProviderService.

Conclusion

Grails is a really neat way to develop applications, and it makes putting together simple web services incredibly easy. In the future, I’ll see how well this scales in the future, but for now it seems like the way to build super quick web applications on the JVM. jQTouch, on the other hand, is rough around the edges, although the potential of native web applications for mobile devices is promising.

tags: , , ,
posted in prog by Stefan Kendall

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org