Category Archives: iPhone

Setting custom request headers with objective resource

For the last few evenings I have been working on a native Mac app for Staging that talks to a Rails restful API. Rather than write the code access the resource URLs myself I thought it would be pretty cool to use Objective Resource, the Objective-c library for accessing Rails-style restful APIs, from the site:

ObjectiveResource is an Objective-C port of Ruby on Rails’ ActiveResource. It provides a way to serialize objects to and from Rails’ standard RESTful web-services (via XML or JSON) and handles much of the complexity involved with invoking web-services of any language from the iPhone.

There are a couple of problems with this approach however. First ObjectiveResource is designed to work on iOS (iPhones, iPod touches and iPads) only, and second it expects access to be controlled by a username and password. The API for Staging uses an X-API-Token HTTP header sent with each request.

Getting ObjectiveResource to work on native Mac

This was pretty simple, just replace all instances of

#import <UIKit/UIKit.h>

with:

#import <AppKit/AppKit.h>

So far nothing has broken.

Getting ObjectiveResource to send a custom header

This was a little trickier and required some more invasive code changes. In Connection.h declare two new methods:

And define them in Connection.m:

Next you need to modify the request that gets called for every GET request:

The addition here is basically a for loop to loop over every header that we have configured and add them to the request object. Finally somewhere before you make any requests in your code:

Done! Now every request that GETs made will have the custom header sent along with it. You can obviously set any number of arbitrary headers using this method too, you aren’t limited to my X-API-Token. It is left as an exercise to the reader to implement the same for POST requests.

It would be nice to see these changes rolled into the ObjectiveResource framework in some way.

Tagged , ,

Recycling Group Finder iPhone app – updates

I’ve pushed a new release of the Recycling Group Finder iPhone app that updates the group database, there should be a lot more Freegle groups available now. Get it from the app store.

Tagged , ,

Freecycle and Freegle group finder for the iPhone

Recycling Group Finder app for the iPhone

I’ve launched my first iPhone app, the Recycling Group Finder for iPhone. It complements the Recycling Group Finder web app making it even easier to find your closest Freecycle or Freegle group by using the iPhone’s in-built GPS. Check out the information page and give it a go, it’s free.

Tagged ,

Generating a plist file in rails

I recently wrote an iPhone app (Waiting for approval in the app store at the time of writing) that needed data exported from a website (recyclinggroupfinder.com). The simplest way of handling external data in an app it seems is using a plist file, so I wrote this to generate one for me.

First of all I made my action respond to the plist format:

Next I created a builder file to format the data:

Then register the MIME type at the bottom of environment.rb:

Mime::Type.register "text/plist", :plist

And that’s it! Well mostly. The XML file generated can be made significantly smaller by converting it into the binary plist format, run this on the command line in terminal after downloading the generated XML plist.

cat things_xml.plist | plutil -convert binary1 - -o things.plist

The resultant binary plist is almost half the size of the XML one, much better for inclusion in an iPhone app:

pleb:~ will$ ls -l things*
-rw-r--r-- 1 will will 1247300 20 Jan 18:50 things.plist
-rw-r--r--@ 1 will will 2110437 20 Jan 18:50 things_xml.plist

Of course it would be much better to generate the binary format directly, and the plist-official gem looks like it can handle that and I mean to investigate, but I wrote the XML version before finding the gem, and it works for me!

Edit: the plist_official gem seems to be gone, but check out the binary plist gem instead.

Tagged , ,

Ninja Tank Army

I’m not sure how stealthy it’s going to be, but a “Ninja Tank Army” is possibly the coolest thing in the world:

Ninja Tank Army

Follow

Get every new post delivered to your Inbox.