Speaker: Laurent Sansonetti
This webinar takes us through RubyMotion, an app development environment that helps you build native apps using the Ruby language. We take a look at how RubyMotion works internally, how it’s different for other mobile toolchains and how we can build cross-platform apps with it.
The session also presents a live demo of Flow, that let's you access both iOS and Android APIs on one single codebase, making it extremely easy to build cross-platform apps.
17. iOS Hello World
class AppDelegate
def application(application, didFinishLaunchingWithOptions:options)
label = UILabel.new
label.text = "Hello World!"
label.sizeToFit
viewController = UIViewController.new
viewController.view.backgroundColor = UIColor.whiteColor
label.center = viewController.view.center
viewController.view.addSubview(label)
frame = UIScreen.mainScreen.applicationFrame
@window = UIWindow.alloc.initWithFrame(frame)
@window.rootViewController = viewController
@window.makeKeyAndVisible
true
end
end
18. iOS Hello World
class AppDelegate
def application(application, didFinishLaunchingWithOptions:options)
label = UILabel.new
label.text = "Hello World!"
label.sizeToFit
viewController = UIViewController.new
viewController.view.backgroundColor = UIColor.whiteColor
label.center = viewController.view.center
viewController.view.addSubview(label)
frame = UIScreen.mainScreen.applicationFrame
@window = UIWindow.alloc.initWithFrame(frame)
@window.rootViewController = viewController
@window.makeKeyAndVisible
true
end
end
19. Android Hello World
class MainActivity < Android::App::Activity
def onCreate(savedInstanceState)
super
text = Android::Widget::TextView.new(self)
text.text = 'Hello RubyMotion!'
self.contentView = text
end
end
20. Android Hello World
class MainActivity < Android::App::Activity
def onCreate(savedInstanceState)
super
text = Android::Widget::TextView.new(self)
text.text = 'Hello RubyMotion!'
self.contentView = text
end
end
23. Static compilation
• RubyMotion apps are native binaries
• For iOS: a native executable
• For Android: a JNI native library
• The original Ruby source code is not present in
the application bundle
• RubyMotion apps weight a couple MB
26. Creating a new project
$ motion create --template=ios Hello
$ motion create —-template=android Hello
$ motion create --template=osx Hello
27. Project configuration
$ vi Rakefile
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'Hello'
end
63. Current libraries
• Net - HTTP networking and host reachability
• JSON - JSON serialization
• Digest - Digest cryptography
• Base64 - Base64 encoding/decoding
• Store - Key-value store
• Location - Location management and (reverse) geocoding
• Task - Lightweight tasks scheduler
• UI - User-interface (big stuff!)
64. Net - GET request
Net.get("https://httpbin.org/get?user_id=1") do |response|
if response.status == 200
response.body['args']['user_id'] # 1
end
end
65. Net - POST request
options = {
headers: {
content_type: :json,
body: { user_id: 1 }
}
}
Net.post("https://httpbin.org/post", options) do |response|
if response.status == 200
response.body['json']['user_id'] # 1
end
end
66. Net - Host reachability
service = Net.reachable?("www.google.fr") do |reachable|
if reachable
# …
end
end
# …
service.stop
71. Location - Monitor
service = Location.monitor do |location, err|
if location
puts location.latitude, location.longitude
puts location.altitude
puts location.time
puts location.speed
puts location.accuracy
else
$stderr.puts err
end
end
# ...
service.stop
72. Location - Reverse geocode
Location.geocode('apple inc') do |location, err|
if location
puts location.name
puts location.address
puts location.locality
puts location.postal_code
puts location.sub_area
puts location.area
puts location.country
else
$stderr.puts err
end
end
73. Task - Timers
timer1 = Task.after 0.5 do
# executed after half second
end
timer2 = Task.every 2.5 do
# executed every two and half seconds
end
# ...
timer2.stop
74. Task - Main thread
Task.main do
# update UI stuff…
end
75. Task - Background thread
Task.background do
# something that takes time…
end
76. Task - Sequential queues
q = Task.queue
q.schedule do
# job 1
end
q.schedule do
# job 2, will be executed after job 1
end
q.schedule do
# job 3, will be executed after job 2
end
q.wait # wait for all jobs to finish
80. UI - Layout
• Lets you layout your user interface with a CSS
(Flexbox) Ruby API
• Very easy to use if you are a Web developer!
• Cross-platform unique implementation
• Does not use iOS’s autolayout or Android layout
classes