官术网_书友最值得收藏!

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
    

    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:

    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.

主站蜘蛛池模板: 甘肃省| 长沙市| 舒城县| 阿瓦提县| 南宫市| 汉阴县| 辰溪县| 如东县| 永济市| 商都县| 红河县| 治县。| 吴旗县| 尤溪县| 赤城县| 瑞昌市| 宜州市| 大渡口区| 射洪县| 酉阳| 衢州市| 达州市| 丹东市| 开化县| 南安市| 铁岭县| 肃南| 齐河县| 义乌市| 长岭县| 正蓝旗| 肇东市| 彰化县| 班戈县| 榆林市| 乡城县| 古田县| 承德市| 梁平县| 辽宁省| 晋中市|