Upgrade to Pro — share decks privately, control downloads, hide ads and more …



Native iOS Apps in Ruby - presented at SDRuby on July 5, 2012


James Miller

July 05, 2012

More Decks by James Miller

Other Decks in Programming


  1. RUBYMOTION Native iOS Apps in Ruby

  2. JAMES MILLER @bensie

  3. LAURENT SANSONETTI • Creator/maintainer of MacRuby • Left Apple, disappeared

    for awhile, re-emerged having created a startup that brings iOS development to Ruby • Plans to contribute parts of this work back to MacRuby
  4. PAIN POINTS When writing native iOS apps

  5. OBJECTIVE-C • Third-party libs still Objective-C, you need to know

    how to read it • RubyMotion syntax adds sanity but doesn't get rid of Objective-C textfromxcode.com
  6. XCODE • Forced into IDE • 11" air is terrible

    with Xcode • Tons of deep-down configuration, lots of dialogs • Buggy / Crashy textfromxcode.com
  7. RUBYMOTION What is it?

  8. WHAT IS IT? • Port of MacRuby for iOS •

    Ruby implementation on top of the Objective-C runtime and iOS foundation classes • Easiest Ruby version you'll ever install
  9. IS IT LIKE PHONEGAP? • No limits on APIs you

    can use because it's not wrapping APIs - the entire SDK is available and you implement Apple APIs directly • It does not bridge objects, it creates Objective-C objects directly • It does not run a bunch of code in a web view • It does not compromise on performance

  11. TRULY NATIVE • Concurrent, no GIL, multi-core processors and GCD

    fully supported • Statically compiled to machine code (MacRuby uses JIT, which is not allowed on devices) • Full App Store compliance • Automatic memory management

  13. LIMITATIONS • require - cannot require files at runtime, they

    need to be available at build-time • eval - not supported in static compilation • define_method - not supported in static compilation • Proc#binding - removed because of performance issue • Regular Ruby gems do not work
  14. LIMITATIONS • No auto-complete - lots of copy-pasting Objective-C method

    names from docs and converting to Ruby • No stdlib • Debugger isn't awesome yet, but it's coming

  16. NAMED PARAMETERS table = UITableView.alloc.initWithFrame(content_frame, style: UITableViewStylePlain) def tableView(tableView, didSelectRowAtIndexPath:indexPath)

    event = @events[indexPath.row] edvc = EventDetailViewController.alloc.init edvc.event = event edvc.title = event.name navigationController.pushViewController(edvc, animated: true) end - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Event *event = [self.events objectAtIndex:indexPath.row]; EventDetailViewController *edvc = [[EventDetailViewController alloc] init]; edvc.event = event; edvc.title = event.name; [self.navigationController pushViewController:edvc animated:YES]; }
  17. BASE64 • Objective-C - find and download a Base64 library,

    add it as a dependency, figure out how to use it, encode your string... • Ruby ["string"].pack("m")
  18. HEADER FILES / PROPERTIES // Foo.h @interface Foo : NSObject

    { } @property (nonatomic, strong) NSString *bar; @end // Foo.m #import "Foo.h" @implementation Foo @synthesize bar = _bar; @end class Foo attr_accessor :bar end
  19. MIX AND MATCH RUBY/OBJ-C METHODS a = [1] a.objectAtIndex(0) #

    => 1 b = NSArray.arrayWithObject(1) b.first # => 1
  20. MIX AND MATCH RUBY/OBJ-C METHODS String.ancestors # => [String, NSMutableString,

    NSString, Comparable, NSObject, Kernel] Hash.ancestors # => [Hash, NSMutableDictionary, NSDictionary, Enumerable, NSObject, Kernel] Array.ancestors # => [Array, NSMutableArray, NSArray, Enumerable, NSObject, Kernel]
  21. ENUMERABLE • #map • #each • #select • #include? •

    #sort_by • #group_by • #take • #...... • #......
  22. BUILT-IN REPL Read eval print loop

  23. REPL • CMD + click on UI element from the

    simulator and make live changes to objects - selected object becomes self in console • Closest thing in XCode is setting a breakpoint

  25. RAKE / COMMAND LINE • Create projects • Test projects

    • Run project in simulator • Send app to device • Create IPA archive for App Store
  26. TESTING You can’t call it Ruby if you can’t properly

    test it
  27. TESTING • Ships with MacBacon (small RSpec clone) • Naive

    at this point, more robust functionality coming soon

  29. TODAY!

  30. FUNCTIONAL VIEW AND CONTROLLER TESTING • Uses the functionality in

    Apple’s UIAutomation framework describe "The Timer view controller" do tests TimerController it "has a timer label" do view('Tap to start').should.not == nil end end

  32. DEPENDENCIES • CocoaPods - Bundler-like dependency definition, automatically vendors and

    requires third-party libraries at build time app.pods do dependency "JSONKit" dependency "AFNetworking" dependency "libPusher" end
  33. BUBBLEWRAP The beginning of Ruby helpers for Cocoa APIs

  34. BUBBLEWRAP • Device • App • JSON • Notification Center

    • Observers • Location • HTTP • EM Reactor • Time • ..... fork and contribute! bubblewrap.io A collection of (tested) helpers and wrappers used to wrap CocoaTouch code and provide more Ruby like APIs.
  35. GETTING STARTED • Buy license for $199 • $99 annual

    renewal • Renewal not required for continued functionality • iOS developer license from Apple ($99/year)
  36. THANKS!