Rapture In Venice, LLC

:: Freelance iOS Development & Team Augmentation

Using RaptureXML in RubyMotion

One of the cooler features of RubyMotion is that you can bring in your favorite 3rd-party Objective-C code. Without this ability, you wouldn’t be able to use essential libraries such as Cocos2D, Sparrow and SDWebImage. Could you imagine?

Although the process RubyMotion requires to bring this code in is easier than you’d expect, it can be tricky and sometimes technical. That being said, if you’re looking to use RaptureXML in ruby form, here’s a sample Rakefile and usage. First, you need to copy the entire RaptureXML source directory into your app’s vendor/ directory. Then, use the following:

Rakefile

# -*- coding: utf-8 -*-
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'

Motion::Project::App.setup do |app|
  app.name = 'ExEmEl'
  app.libs << '/usr/lib/libxml2.dylib'
  app.vendor_project "vendor/RaptureXML", :xcode

end

Source Code

    xml = <<-eos
      <scores>
          <student name="Jane">80</student>
          <student name="John">95</student>
          <student name="Joe">60</student>
      </scores>
    eos

    # load xml
    rxml = RXMLElement.elementFromXMLString(xml, encoding:NSUTF8StringEncoding)

    # calculate average test score
    total_score = num_scores = 0

    rxml.iterate("student", usingBlock:lambda { |e|
      total_score += e.textAsInt
      num_scores += 1 
    })    

    NSLog("Average score: #{total_score / num_scores}")

After updating the vendor code specification, I like to run a rake clean because sometimes RubyMotion doesn’t pick up the code correctly if you don’t. If you did everything right, you now have the power of RaptureXML in RubyMotion!

  • Print
  • Facebook
  • Twitter

John Blanco

John Blanco is a freelance iOS developer living in Lakewood, Colorado. He's been developing mobile apps for over 15 years, beginning in the medieval days of Java ME and Blackberry and all the way through iPhone and Android! He's led development on dozens of apps across a wide variety of domains such as retail, vision, orthotics, games, sports, and more!

More Posts - Website

Follow Me:
LinkedIn

, ,

Comments are currently closed.