iPhone/iPad Tutorial

1. Hello World

Lets build an iOS app called 'Hello World'.

Open your terminal and go to a place where you would like this first project to be created, then type the following command.

$ motion create Hello

This command will create a RubyMotion project in a new directory named Hello. If this directory already exists or cannot be created, the command will fail.

Let’s have a look inside.

$ cd Hello
$ ls
Rakefile app resources spec

A RubyMotion project is Rakefile-based. rake is the de-facto Ruby build program. It is similar to make and it ships with Mac OS X by default.

The app directory contains the application code. The resources directory will eventually contain the resource files of your project, such as icon, image or sound files. The spec directory contains specification/test files.

Let’s run the default task.

$ rake

This should build our project then start the simulator, and you should see…​ an empty, black window. It’s actually normal, we haven’t written any code yet!

If you look inside the app directory you will see an app_delegate.rb file, which is created by default. This file implements the AppDelegate class, which is responsible for controlling your application.

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    true
  end
end

Open the app/app_delegate.rb file with your favorite editor. We will change the code to do something more interesting, such as triggering an alert.

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    alert = UIAlertView.new
    alert.message = "Hello World!"
    alert.show
    true
  end
end

If you run the rake command again from the terminal you should be able to see the alert in the simulator.

Hello World

Now, let’s try this outside the simulator. Make sure you have an iOS device properly configured for development connected over USB, and type the following.

$ rake device

This should install the Hello app on your device. You can now pick it up and run the app, and you should be able to see the alert message.

Congratulations, you successfully created your first RubyMotion program. That wasn’t too hard, was it?

2. And Now?

Congratulations, you successfully created your first RubyMotion iOS app. That wasn’t too hard, was it?

To continue, we recommend that you check the Samples page as well as the Sample Code Repository on GitHub. Each of the sub-folders contains a RubyMotion project as introduced above. You can type rake in each directory to build and run them and check their source code by reading the files in the 'app' directory.