Getting data in rails ready for the iPhone with Property Lists (.plist)
Took me awhile to finally figure out exactly what the Property Lists were all about, once it came together, putting together a nice property list was pretty easy. The problem I'm solving is giving a fairly static list of teams to an iPhone app. The iPhone parses out binary plist files the most efficiently, so here it is if you want to generate these plists from a rails app:
First get an array of all the objects you want to put into the property list and create an plist array
teams = Team.find(:all) plist_array = Array.new teams.each do |team| plist_array << {:id => team.id, :name => team.display_name} end
This creates a nice array with a few attributes we want to add. If you want all attributes added to the plist_array, instead of adding in a hash just add in team.attributes, which returns a hash of all attributes. What comes next is the use of the plist gem
$ sudo gem install plist
The above will install the plist gem onto your machine, make sure it's unpacked and/or avai lable on your production servers if you want this to be performed there as well. Once you have the gem installed, require the gem in your controller and you can use the .to_plist to basically dump the plist_array into the accepted XML Property List format.
plist_array.to_plist
This of course does not give you a binary file yet. For that, use the plutil that is usually installed on mac osx machines. Mine just came with it. If your machine doesn't have the util, it's part of the developer tools on your OSX install disks.
$ plutil -convert binary1 filename.plist
The option -convert basically takes one conversion parameter, either being binary1 or xml1. binary1 converts the file, designated in the next parameter (in our case filename.plist), that contains the output of the to_plist. The to_plist outputs the XML format of the Property List, and -convert binary1 will convert it to a nice binary representation. You can go backwards with -convert xml1
$ plutil convert xml1 filename.plist
When it converts, it resaves the file, filename.plist for example. Once you get that binary file, you can use it easily on the iPhone for retrieving the data.
For some more examples of how people are using the plist gem see this post to get you started: http://stackoverflow.com/questions/1264329/getting-active-records-to-display-as-a-plist

