Chapter 2. Instant Gratification – Your First Application

"Dream the impossible, seek the unknown, and achieve greatness."

Anonymous

Now that we are all charged up about RubyMotion and have our system set up, let's create a simple RubyMotion application. We will try and keep it simple, but sometimes you may feel disconnected by monotonously typing the code. Although, going along is enough for now. Remember that mimicry is a powerful form of learning; that's how we have learned most of our skills, such as talking, reading, writing, and that is how you will learn to program with RubyMotion. We promise you that by the end of this book, you will have sufficient knowledge of RubyMotion to create an iOS application and make it live on the App Store. In this chapter we will cover the following topics:

  • Creating your first RubyMotion application
  • Understanding the folder structure
  • Exploring the command line
  • Configuring your application
  • REPL – the interactive console
  • The debugger

Your first application

Let's start with the classic HelloWorld application. As we have discussed in the last chapter, RubyMotion has a terminal-based flow, so let's fire up our terminal and create our very first RubyMotion application.

$motion create HelloWorld
Create HelloWorld
    Create HelloWorld/.gitignore
    Create HelloWorld/Rakefile
    Create HelloWorld/app
    Create HelloWorld/app/app_delegate.rb
    Create HelloWorld/resources
    Create HelloWorld/spec
    Create HelloWorld/spec/main_spec.rb

If you observe closely the output on the terminal screen, you will see that a lot of files and directories have been generated by a single motion command, which automatically creates standard directories, and you will also see the file structure that will quickly bring us onboard with app development, which we can work on later and enhance to make a fully functional application. Moreover, since the structure is common to all the RubyMotion apps, it's easy to understand.

Note

Just like the motion command, popular frameworks such as Ruby on Rails also have commands such as rails to create a predefined layout of the application.

The following steps automatically compile the code and start the application on a simulator:

  1. Start the application, traverse to the application directory, and type the following command:
    $ cd HelloWorld
    $rake
    Build ./build/iPhoneSimulator-6.0-Development
    Compile ./app/app_delegate.rb
    Create ./build/iPhoneSimulator-6.0-Development/HelloWorld.app
    Link ./build/iPhoneSimulator-6.0-Development/HelloWorld.app/HelloWorld
    Create ./build/iPhoneSimulator-6.0-Development/HelloWorld.app/Info.plist
    Create ./build/iPhoneSimulator-6.0-Development/HelloWorld.app/PkgInfo
    Create ./build/iPhoneSimulator-6.0-Development/HelloWorld.dSYM
    warning: no debug symbols in executable (-arch i386)
    Simulate ./build/iPhoneSimulator-6.0-Development/HelloWorld.app
    
    Your first application

    Wow! The rake command automatically compiles the code and starts the application on a simulator. So far, we have not created any views for our application; that's why we can see a blank screen. It looks boring, but remember that we have not written a single line of code. So let's write some code, create some views, and build our application again.

    Tip

    You can open the RubyMotion project in your favorite editor. If you don't have an editor yet, you can use either TextEdit or VIM.

  2. Open the file app_delegate.rb in the app folder and add the following code in it:
    class AppDelegate
      def application(application, didFinishLaunchingWithOptions:launchOptions)
       alert = UIAlertView.new
       alert.message = "Hello World!"
       alert.show
       true
      end
    end
  3. Let's re-run our application by traversing to the application directory and typing the execute command (rake):
    $rake
    

    The rake command will compile our code and fire up the iPhone simulator. We can see a blue pop-up saying Hello World! in the following screenshot:

    Your first application

    Let's understand the code that we have written in AppDelegate. Here the application method (didFinishLaunchingWithOptions:launchOptions) is called first when our application starts. This will be the starting point of our application and the right place to define our window.

    RubyMotion functions are a combination of the usual Ruby name method (didFinishLaunchingWithOptions) with their named parameters; a variable directly follows the function, which it refers to, and therefore, we don't need to know the implementation of the function.

    Note

    Named parameters were added to RubyMotion to preserve the existing Objective-C APIs, and the extra symbols are required parts of the method name, for example, didFinishLaunchingWithOptions:launchOptions.

    As discussed, the code written in AppDelegate will be called automatically as the application is initialized.

    In the following code snippet, we created an object alert of the UIAlertView class and then we assigned a Hello World! string to the message attribute of the object. Now we have our alert object ready. To display this alert on the device screen, we call the show method on the alert object as follows:

       alert = UIAlertView.new
       alert.message = "Hello World!"
       alert.show

    UIAlertView is a class that is bundled in the UIKit framework of the iOS. We can use this class to display an alert message on the screen. This class is inherited from UIView that is inherited from UIResponder that, in turn, is inherited from NSObject.

    Note

    Why do we see the NS prefix?

    Objective-C is a superset of C and thus doesn't have namespaces like in C++; therefore, the symbols must be prefixed with a unique prefix so that they don't collide. This is particularly important for symbols defined in a framework. The original code for the Cocoa frameworks came from the NextStep libraries, and so the NextStep engineers chose to prefix their symbols with NS.

  4. To exit the application, close the simulator by selecting the exit option or press Command + Q.

    Tip

    The iOS simulator is a great tool for testing your applications quickly. It comes bundled with Xcode. But you can't test everything on the simulator. To test the shaking of a device, camera, GPS, Accelerometer, Gyroscope, and other device capabilities, you may require additional products to pass device data to the app in the simulator.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.188.241