Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
RubyMotion iOS Development Essentials
RubyMotion iOS Development Essentials

RubyMotion iOS Development Essentials: Forget the complexity of developing iOS applications with Objective-C; with this hands-on guide you'll soon be embracing the logic and versatility of RubyMotion. From installation to development to testing, all the essentials are here.

£28.99 £19.99
Book Jul 2013 262 pages 1st Edition
eBook
£28.99 £19.99
Print
£37.99
Subscription
£13.99 Monthly
eBook
£28.99 £19.99
Print
£37.99
Subscription
£13.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Jul 16, 2013
Length 262 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781849695220
Category :
Languages :
Table of content icon View table of contents Preview book icon Preview Book

RubyMotion iOS Development Essentials

Chapter 1. Getting Ready for RubyMotion

"You will never win if you never begin."

Helen Rowland

Welcome to RubyMotion iOS development essentials. The goal of this book is to quickly acquaint you with RubyMotion and start building applications for your favorite iOS device. Ever since the introduction of the first iPhone, followed by the iPad, iOS devices have become very popular because of the way they have revolutionized how people work, and thereby have begun an era of increased productivity. The success behind the phenomenal growth of these devices lies in the applications bundled with them, which increases their functionality exponentially.

We will learn how to develop iOS applications with RubyMotion by building sample applications from scratch. We will try to have something tangible with a running code by the end of every chapter so that you can see a clear progression from chapter to chapter. Though RubyMotion and iOS Cocoa APIs are vast, and part of a fast-moving framework, we'll focus on the smaller, more stable set of core RubyMotion techniques that have crystallized after its release. This means that the knowledge you gain here will not become obsolete quickly. This book is written keeping the Zero-to-Deployment approach in mind.

In this chapter we will learn:

  • Various ways to create iOS applications

  • How RubyMotion is different from other frameworks

  • RubyMotion installation

How can I develop an iOS application?


To develop iOS applications, there are various third-party frameworks available, apart from Apple libraries. If we broadly categorize the ways in which we can create iOS applications, we can divide them into three ways.

Native apps using Objective-C

This is the most standard way to build your application, by interacting with Apple APIs and writing apps in Objective-C. Applications made using native Apple APIs can use all possible device capabilities, and are relatively more reliable and high performing (however, the topic of performance is debatable based on the quality of the developer's code).

Mobile web applications

Mobile web applications are simple web applications extended for mobile web browsers, which can be created using standard web technologies such as HTML5. For example, if we browse through http://www.twitter.com in a mobile browser, it will be redirected to http://mobile.twitter.com, which renders its corresponding views for mobile devices. These applications are easy to create but the downside is that they have limited access to user data (for example, phonebook) and hardware (for example, camera).

Hybrid applications

These applications are somewhere in between mobile web apps and native applications. They are created using common web technologies such as HTML5 and JavaScript and have the ability to use device capabilities via their homegrown APIs. Some of the popular hybrid frameworks include Rhomobile and Phonegap.

If we compare the speed of development and user experience, it can be summed up with the following diagrams:

From the preceding diagrams we see that mobile web apps can be created very quickly but we have to compromise on user experience. While native apps using Objective-C have good user experience, they have a very steep initial learning curve for web developers.

RubyMotion is good news for both users and developers. Users get an amazing experience of a native application and developers are able to develop applications rapidly in comparison to applications developed using Objective-C. Let's now learn about RubyMotion.

What is RubyMotion?


RubyMotion is a toolchain that allows developers to develop native iOS applications using the Ruby programming language. RubyMotion acts as a compiler that interacts with the iOS SDK (Software Development Kit). This gives us enormous power to make use of Apple libraries; therefore, once the application has compiled and loaded, the device has no idea whether it's an application made using Objective-C or RubyMotion.

RubyMotion is a product of HipByte, founded by Laurent Sansonetti.

Note

Laurent Sansonetti is a former Apple employee and the brain behind MacRuby. MacRuby is the implementation of Ruby on top of Mac OS X core technologies, which was maintained by Apple for over 4 years. And the best part is that RubyMotion is based on MacRuby.

While developing applications with RubyMotion using Ruby, you always have access to the iOS SDK classes. This gives you the benefit of even mixing Objective-C and Ruby code, as RubyMotion implements Ruby on top of the Objective-C runtime and iOS Foundation classes.

This is how a typical RubyMotion application works. The code written in RubyMotion is fully compiled into machine code, so the application created by RubyMotion is as fast as the one created using Objective-C.

Why RubyMotion?


So far we have learned what RubyMotion is, but the question that comes to mind is, why should we use RubyMotion? There are many reasons why RubyMotion is a good choice for building robust iOS apps. The following sections detail a few that we think matter the most.

If you are not an Objective-C fan

For a newbie developer, Objective-C is an arduous affair. It's complicated to code; even for doing a simple thing, we have to write many lines of code. Though it is a powerful language and one of the best object-oriented ones available, it is time consuming and the learning curve is very steep. On the other hand, Ruby is more expressive, simple, and productive in comparison to Objective-C. Because of its simplicity, developers can shift their focus onto problem solving rather than spending time on trivial stuff, which is taken care by Ruby itself. In short, we can say RubyMotion allows us to use the power of Objective-C with the simplicity of Ruby.

Ruby classes used in RubyMotion are inherited from Objective-C classes. If you are familiar with the concept of object-oriented programming, you can understand its power. This means we can directly use Apple iOS SDK classes from your RubyMotion code. We will be discussing more on this in the next chapter.

Tip

Ruby classes in RubyMotion have the same ancestor as Objective-C.

It is not a bridge

RubyMotion apps get direct access to iOS SDK APIs, which means the size of application and performance created using RubyMotion is comparable to the one created using Objective-C. It implements Ruby on top of the Objective-C runtime and iOS Foundation classes. RubyMotion uses a state-of-the-art static compiler based on Low Level Virtual Machine (LLVM), which converts the Ruby source code into a blazing fast machine code. The original source code is never present in the application bundle. A typical application weighs less than 1 MB, but the size can increase depending on the use case.

Managed memory

One of the key features of RubyMotion is that it takes care of memory management. Just like ARC (Automatic Reference Counting) with Xcode 4.4 and above, we don't have to take the pain of releasing the memory once an object is no longer used. RubyMotion does the magic and we don't need to think about it. It handles it on its own.

Terminal-based workflow

RubyMotion has a terminal-based workflow; from creation of the application to deployment, everything can be done through terminals. If you are used to working on terminals, you know it adds to speedier development.

Easy debugging with REPL

The terminal window where you run Rake also gives you the option to debug with REPL (Read Evaluate Print Loop), which lets you use Ruby expressions that are evaluated on the spot, and the results are reflected on the simulator while the application is still running. The ability to make live changes to the user interface and internal application data structures at runtime is extremely useful for testing and troubleshooting issues with the application, as this saves a lot of time and is much faster than a traditional edit-compile-run loop. If this confuses you right now, don't worry, as we will discuss more on this powerful feature in later chapters.

Tip

REPL is similar to IRB in Ruby.

It is extendable

We can use RubyMotion salted gems easily by just adding them in the Rakefile. What are RubyMotion salted gems? We can't use all the gems that are available for Ruby right now, but there are a lot of gems specifically developed for RubyMotion. As the RubyMotion developer community expands, so will its gem bouquet, and this will make our application development rapid.

Third-party Objective-C libraries can be easily used in a RubyMotion project. It supports CocoaPods, which is a dependency manager for Objective-C libraries, making this process a bit easier.

Debugging and testing

RubyMotion has a console-based inbuilt interactive debugger for troubleshooting the issues both on a simulator and on a device using GDB (GNU Debugger). GDB is extremely powerful on its own, and RubyMotion uses it for debugging the compiled Ruby code. Also, RubyMotion projects are fit for Test Driven Development (TDD). We can write a unit test for our code from the beginning. We can use Behavior Driven Development (BDD) with RubyMotion, which is integrated into every project. We will discuss more about testing in later chapters.

Tip

RubyMine editor also supports RubyMotion and it has a very powerful debugging mechanism through breakpoints.

Pop quiz


Q.1. How can we distinguish between the iOS application created by RubyMotion and the iOS application created by Objective-C?

  1. You can distinguish based on the user experience of the application.

  2. You can distinguish based on the performance of the application.

  3. You can't distinguish based on the user experience and performance of the application.

    Solution: If your answer was option 3, you were right. We can't distinguish between applications created by RubyMotion or Objective-C as the user experience and performance are similar.

Q.2. How can we extend RubyMotion?

  1. We can use Objective-C libraries.

  2. We can use all Ruby gems.

  3. We can use RubyMotion-flavored gems.

  4. We can't use any other libraries.

    Solution: If your answer was option 1 and 3, you were right. Yes, we can use Objective-C libraries and also RubyMotion-flavored gems.

RubyMotion installation – furnish your environment


Now that we have got a good introduction to RubyMotion, let's set up our development environment; but before that let's run through some of the prerequisites.

Prerequisites for RubyMotion

  • You need a Mac OS: we can't develop iOS applications with RubyMotion on any other operating system; so we definitely need a Mac OS.

  • OSX 10.6 or higher: RubyMotion requires a Mac running OSX 10.6 or higher. OSX 10.7 Lion is highly recommended.

  • Ruby: the Ruby framework comes preinstalled with Mac OS X. If you have multiple versions of Ruby, we recommend that you use Ruby Version Manager (RVM). For more details, visit https://rvm.io/.

  • Xcode: next we need to install Xcode, which includes the iOS SDK, developed by Apple and essential for developing iOS applications. It can be downloaded from the App Store for free. It also includes the iPhone/iPad simulator, which will be used for testing our application.

  • Command Line Tools: after installing the Xcode toolchain, we need to install the command-line tools package, which is necessary for RubyMotion. To confirm that command-line tools is installed with your Xcode, open Xcode in your Applications folder, go to the Preferences window, and click on the Downloads tab. You should see the Command Line Tools package in this list. If it is not yet installed, make sure to click on the Install button.

Tip

If you have an old version of Xcode, run the following command on the terminal:

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

This command will set up the default Xcode path.

Installing RubyMotion

RubyMotion installation is really simple and takes no time at all. RubyMotion is a commercial product that you need to purchase from www.rubymotion.com. Once purchased, you will receive your unique license key and installer.

RubyMotion installation is a five-step procedure and is given here:

  1. Once you have received the package, run the RubyMotion installer as follows:

  2. Read and accept the EULA (End User License Agreement).

  3. Enter the license number you have received as shown in the following screenshot:

  4. Time for a short break—it will take a few minutes for RubyMotion to get downloaded and installed on your system. You can relax for some time.

  5. Yippee!! There is no step 5. And that's how quick it is to start working with RubyMotion.

Update RubyMotion

RubyMotion is a fast-moving framework and we need to upgrade it once there is a new release available. Upgrading RubyMotion is again really simple—with one command, you can easily upgrade it to the latest version.

sudo motion update

You need to be connected to the Internet for an upgrade to happen.

Note

If you want to work on an old version, you can downgrade using the following command:

sudo motion update –force-version=1.2

But we recommend using the latest version.

How do we check we've done everything correctly?

Now that we have installed our RubyMotion copy, it's good practice to confirm which version we have installed; to do this, go to the terminal and run the following:

motion –v

This command outputs the RubyMotion version installed on your machine. If you get an error, you need to reinstall.

Pick your own editor – you are not forced to use Xcode

With RubyMotion, you are not forced to use Xcode. As every developer is more comfortable with a specific editor, you are open to choose what you like. However, we recommend the following editors for Ruby development:

  • RubyMine

  • Vim

  • TextMate

  • Sublime

  • Emacs

Tip

RubyMine now provides full support to a RubyMotion project.

How to get help

If you are facing some issues, the preferred way to get a solution is to discuss it at the RubyMotion Google group, (https://groups.google.com/forum/?fromgroups#!forum/rubymotion), where you can interact with fellow developers from the community and get a speedy resolution.

Sometimes you might not get a precise response from the RubyMotion group. Not to worry, RubyMotion support is there to rescue you. If you have a feature request, an issue, or simply want to ask a question, you can log a support ticket—that too from the command line using the following command:

$ motion support

This will open up a new window in your browser. You can fill and submit the form with your query. Your RubyMotion license key, email address, and environment details will be added automatically.

Tip

The RubyMotion community is growing at a very fast pace. In a short span of time, a lot of popular RubyMotion gems have been created by developers.

FAQs


We believe no question is silly. By now you will have many questions in your mind regarding RubyMotion. We have tried to answer a few of the most frequently asked questions (FAQs) related to topics covered so far in this section. Here are a few of them:

Q1. Are the applications created by RubyMotion in keeping with Apple guidelines?

Answer. Yes, RubyMotion strongly follows the review guidelines provided by Apple. Many applications created using RubyMotion are already available at the App Store.

Q2. Will my RubyMotion application work on a Blackberry, Android, or Windows phone?

Answer. No, applications created using RubyMotion are only for iOS devices; it is an alternative to programming in Objective-C. For a single-source multi-device application, we would recommend hybrid frameworks such as Rhomobile, Phonegap, and Titanium. For android development using Ruby, you can try Rubuto.

Q3. Can I share an application with someone?

Answer. Yes and no. With the Apple Developer Program membership, you can share your application only for testing purposes with a maximum of 100 devices, where each device has to be registered individually with Apple. Also, you cannot distribute your application on the App Store for testing. Once you have finished developing your application and are ready to ship, you can submit it to Apple for an App Store review.

Q4. Can I use Ruby gems?

Answer. Yes and no. No because we can't use normal Ruby gems, which you generally use in your Ruby on Rails projects; and yes because you can use gems that are specifically developed for RubyMotion, and there are already many such gems.

Q5. Will my application work on iPad and iPod Touch?

Answer. Absolutely, your application will work on any iOS devices, namely iPhone, iPad, and iPod Touch.

Q6 Is Ruby allowed on the App Store?

Answer. The App Store can't distinguish between applications made using Objective-C and those made using RubyMotion. So, no worries, our RubyMotion applications are fit for the App Store.

Q7. Can I use third-party Objective-C libraries?

Answer. Certainly. Third-party Objective-C libraries can be used in your project. RubyMotion provides integration with the CocoaPods dependency manager, which helps in reducing the hassle. You also can use C/C++ code provided that you wrap it into the Objective-C classes and methods.

Q8. Is RubyMotion open source?

Answer. RubyMotion as a toolchain is open source (available at GitHub). The closed source part is the Ruby runtime, which is, however, very similar to MacRuby runtime (which is open source).

Summary


Before we move to the next chapter, let's review all that we have learned so far. We first discussed the different ways to create iOS applications. Then we started with RubyMotion and discussed why to use it. And in the last section, we learned how to get started with RubyMotion and which editor fits with it.

Now that we have our RubyMotion framework up and running, the next obvious task is to create our very first application, the most rudimentary Hello World application. In the next chapter, we will also learn the structure of the RubyMotion application in general.

Left arrow icon Right arrow icon

Key benefits

  • Get your iOS apps ready faster with RubyMotion
  • Use iOS device capabilities such as GPS, camera, multitouch, and many more in your apps
  • Learn how to test your apps and launch them on the AppStore
  • Use Xcode with RubyMotion and extend your RubyMotion apps with Gems
  • Full of practical examples

Description

RubyMotion is a revolutionary toolchain for iOS app development. With RubyMotion, you can quickly develop and test native iOS apps for the iPhone and iPad, combining the expressiveness and simplicity of Ruby with the power of the iOS SDK. "RubyMotion iOS Development Essentials" is a hands-on guide for developing iOS apps using RubyMotion. With RubyMotion, you can eliminate the complexity and confusion associated with the development of iOS applications using Objective-C. We'll begin from scratch. Starting by installing RubyMotion, we'll build ourselves up to developing an app that uses the various device capabilities iOS has to offer. What's more, we'll even learn how to launch your app on the App Store! We'll also learn to use iOS SDK classes to create application views. Discover how to use the camera, geolocation, gestures, and other device capabilities to create engaging, interactive apps. We'll develop stunning user interfaces faster with the XCode interface builder and make web apps by using WebView. We'll then augment applications with RubyMotion gems, doing more by writing less code and learn how to write test cases for RubyMotion projects. Finally, we'll understand the app submission process to push your app to Apple's App Store With "RubyMotion iOS Development Essentials", we will learn how to create iOS apps with ease. At the end of each chapter we will have a tangible and running app, which utilizes the concepts we have learnt in that chapter.

What you will learn

Install RubyMotion and get a feel for the toolchain Understand the evolution of Rubymotion from Objective-C : compare RubyMotion syntax with the corresponding Objective-C syntax Work with the XCode interface builder and design stunning user interfaces with RubyMotion Augment applications with gems: Use and create gems for RubyMotion Use device capabilities including the camera, geolocation, gestures and address book in your apps Show your web apps right from your iPhone window with WebView Learn to store data offline with Core Data Get your apps ready for the App Store!

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Jul 16, 2013
Length 262 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781849695220
Category :
Languages :

Table of Contents

19 Chapters
RubyMotion iOS Development Essentials Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Authors Chevron down icon Chevron up icon
Acknowledgement Chevron down icon Chevron up icon
About the Reviewers Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
Getting Ready for RubyMotion Chevron down icon Chevron up icon
Instant Gratification – Your First Application Chevron down icon Chevron up icon
Evolution – From Objective-C to RubyMotion Chevron down icon Chevron up icon
Mastering MVC Paradigm Chevron down icon Chevron up icon
User Interface – Cosmetics for Your App Chevron down icon Chevron up icon
Device Capability – Power Unleashed Chevron down icon Chevron up icon
Interface Builder and WebView – More Goodies! Chevron down icon Chevron up icon
Testing – Let's Fail Gracefully Chevron down icon Chevron up icon
Creating a Game Chevron down icon Chevron up icon
Getting Ready for the App Store Chevron down icon Chevron up icon
Extending RubyMotion Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.