Gavin Morrice

is a web and iOS developer from Edinburgh, Scotland.

more about me »

Blog Archive

Reading and writing to plist files in Rubymotion

In two of the projects I'm working on just now I'm using plist files to store data. To save me from having to duplicate work in both apps, I created a module to make light work of reading and writing to plist files.

http://github.com/KatanaCode/PListReadWrite/

Installation

When I'm creating Ruby modules to share across more than one app I prefer to install the modules as git submodules. That way if I need to update/patch I only need to update once, not in every app!

# From your project root dir
git submodule add git@github.com:KatanaCode/PListReadWrite.git lib/plist_rw

Alternatively you can just copy/paste the file directly to your app.

Example Usage

Lets assume we have this plist file in our resources directory...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>jim</key>
    <dict>
      <key>name</key>
      <string>Jim</string>
      <key>email</key>
      <string>jim@jimsemail.com</string>
    </dict>
    <key>jane</key>
    <dict>
      <key>name</key>
      <string>Jane</string>
      <key>email</key>
      <string>jane@janesemail.com</string>
    </dict>
  </dict>
</plist>

... and we want to copy it over to our app's documents directory so we can update/edit the plist.

# Check if the plist exists in our documents dir
PListRW.exist?(:users, directory = :documentsDir) # => false

# Check if the plist exists in our main bundle
PListRW.exist?(:users, directory = :mainBundle) # => true

# Copy the plist file from the main bundle to the documents dir
PListRW.copyPlistFileFromBundle(:users)

# Fetch the object from the plist file
@users_hash = PListRW.plistObject(:users, Hash) # => A hash containing the User data

# Update the data
@users_hash[:jim] # => { name: "Jim", email: "jim@jimsemail.com" }
@users_hash[:jim][:name] = 'James'

# Store the data back in the plist
PListRW.updatePlistFileWithObject(:users, @users_hash)

# Check this worked OK
PListRW.plistObject(:users, Hash)[:jim][:name] # => "James"

How to load one Gemfile from another Gemfile in Ruby

I'm working on a Rubymotion project just now in which most of the application code is held within a submodule (so it can be shared across multiple apps).

The submodule has it's own gem dependencies and it's own Gemfile.

I was looking for a way to load the submodule's Gemfile from within the main application's Gemfile and I discovered I can do this:

# in my main app's Gemfile
source 'http://rubygems.org/'

eval_gemfile File.join(File.dirname(__FILE__), "vendor/MyPlugin/Gemfile")

gem "some_other_gem"

It's that easy


How To Use the Facebook iOS SDK In Your RubyMotion project

Here's a quick how-to to set up the Facebook iOS SDK in a RubyMotion project.

  1. Install Bundler if you haven't already.
    run bundle init from the terminal
    Add require 'bundler' to your Rakefile under require 'motion/project'
    Add Bundler.require under that.

  2. Install Cocoapods if you haven't already.
    Add gem 'motion-cocoapods' to your Gemfile

  3. Then add the following to your Rakefile...

    app.frameworks += %w{ AdSupport Accounts Social }
    app.weak_frameworks += %w{ AdSupport Accounts Social }
    app.pods do
      pod 'Facebook-iOS-SDK', '~> 3.1.1'
    end

    app.info_plist['FacebookAppID'] = '<your app id>'
    app.info_plist['URL types'] = { 'URL Schemes' => 'fb<your app id>'} # note the "fb" prefix

Fin!

I've uploaded an example application to Github: Bodacious/FacbookConnectWithRubyMotion