Getting started with RubyMotion for iOS development

What is RubyMotion?

RubyMotion is a toolchain which allows you to write truly native apps for iOS, Android, OSX, Apple Watch, and AppleTV, in a syntax that looks like Ruby. It is not a transpiler (converting your code into Objective-C). Your app is statically compiled in the same way that Objective-C or Swift compile down to native code. RubyMotion is essentially a new implementation of Ruby, designed for building mobile apps.

Why use RubyMotion?

One of the main benefits of using RubyMotion is that you don’t have to learn a new language in order to start building mobile apps. You get to use the Ruby language that you are already familiar with. If you’re not already familiar with Ruby, you will find Ruby a lot easier to read and write than Objective-C, Swift, or Java. Practically speaking, you will end up learning a little bit about Objective-C in order to translate some of the examples or documentation.

A second benefit is the ability to build cross platform apps. By removing the language differences, RubyMotion has the potential to allow you to write a single app that supports both iOS and Android within the same codebase. Practically speaking, building cross-platform apps in a single codebase depends on using some sort of framework that abstract the differences between platform-specific user interfaces. Fortunately, there are already a few great frameworks that are making progress in this area. Regardless, there is still the opportunity for reusing code because of the common language.

Installing RubyMotion

It should be noted that RubyMotion only runs on a Mac. This is because RubyMotion still needs access to Apple libraries and build tools in order to compile your app and run the iOS simulator. You will also need to make sure you have Xcode installed. Once you have Xcode installed, open Xcode > Preferences, click on the Downloads tab, and install iOS SDK 9.2.

To get started using RubyMotion, you first need to register for a license and download the installer. Visit the RubyMotion download page and select between the following plans:

  • Free (“starter”) — Great for trying out RubyMotion. You can even publish an app to the App Store, but your launch image will display a message saying that it was “Made with RubyMotion”. Also note that the free plan only supports the latest version of iOS and Android.
  • Indie ($199/year) — You will likely want this one if you plan to do any sort of professional development. Support is limited to the community forums (and our community Slack channel).
  • Professional ($499/year) — If you would like priority support, this is the plan for you.

Once you’ve registered for a plan, you will receive an email with your license key and a link to download the installer.

Generating your first app

Now that you have RubyMotion installed, let’s generate your first app. Open up your Terminal app. Note that this tutorial was written using RubyMotion 4.8.

RubyMotion comes with quite a few templates for starting your project. To see all the templates, type motion create:

The iOS template is the default, so start with this:

This will create a directory structure that looks like this:

The Rakefile is the entry point into your RubyMotion app. Open it up and you will find the configuration information for your app. It starts by loading the RubyMotion project template file, which adds all of the Rake tasks which will be used for compiling the app.

To run the app in the iOS simulator, run the default rake task:

If all goes well, you should see the iOS simulator launch and display a screen that looks like this:

If you didn’t see the simulator pop up, then it’s time to figure out what went wrong. I might have forgotten to explain a step. Here are a few places you can try to find help:

Assuming you’ve made it this far, then you’ve resolved any system configuration and dependencies necessary to develop your RubyMotion application.

Next, let’s take a quick look at the app_delegate.rb file. This is the entry point into your iOS app.

In the sample App Delegate that was generated, you can see the following:

  • Create a view controller.
  • Create a navigation controller with the view controller as the root.
  • Create a window with the nav controller as the root.

RubyMotion makes this code a lot easier to read than the Objective-C equivalent. However, it’s still quite a bit of boilerplate code to accomplish very little and doesn’t feel much like the kind of Ruby code you’re used to writing. Fortunately, there are a few different frameworks available which greatly simplify a lot of the iOS boilerplate code.

In the next tutorial, we will introduce you to a great RubyMotion framework called RedPotion which will significantly improve your RubyMotion iOS development experience.

Getting started with RedPotion for faster iOS development


This tutorial originally appeared on the Portland RubyMotion Meetup blog. If you’re in the Portland area, come join us at our next meetup!