- RubyMotion iOS Develoment Essentials
- Abhishek Nalwaya Akshat Paul
- 749字
- 2021-08-13 16:43:54
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:
- 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.
- 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
- 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 aHello 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 theUIKit
framework of the iOS. We can use this class to display an alert message on the screen. This class is inherited fromUIView
that is inherited fromUIResponder
that, in turn, is inherited fromNSObject
.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.
- 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.
- Instant Testing with CasperJS
- GitLab Cookbook
- Visual Basic 6.0程序設計計算機組裝與維修
- Django:Web Development with Python
- Ray分布式機器學習:利用Ray進行大模型的數據處理、訓練、推理和部署
- 零基礎學MQL:基于EA的自動化交易編程
- 假如C語言是我發明的:講給孩子聽的大師編程課
- R的極客理想:工具篇
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- 精通網絡視頻核心開發技術
- TypeScript項目開發實戰
- Scratch3.0趣味編程動手玩:比賽訓練營
- Android開發三劍客:UML、模式與測試
- Data Science Algorithms in a Week
- 計算機應用基礎(第二版)