98
Building Full-Fledged Native Apps Using RubyMotion Laurent Sansonetti - @lrz HipByte

[Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Embed Size (px)

Citation preview

Page 1: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Building Full-Fledged Native Apps Using RubyMotion

Laurent Sansonetti - @lrz HipByte

Page 2: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Myself• Laurent Sansonetti

• Software hacker

• Company founder

Page 3: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Ruby programmer since 2002

Page 4: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Apple 2004-2012

Page 5: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

2012-now

Page 6: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

RubyMotion

Page 7: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Toolchain to write native mobile apps in

Ruby

Page 8: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Cross-platform: iOS and Android

(and OS X)

Page 9: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Designed for Ruby programmers

Page 10: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Consistent experience for cross-platform

native development

Page 11: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

iOS Android

Language Objective-C Swift Java

Environment Xcode Android Studio

APIs iOS SDK Android SDK

Page 12: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

iOS Android

Language Ruby

Environment Your Editor + Terminal

APIs iOS SDK Android SDK

Page 13: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Unified Ruby runtimes

Page 14: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Custom implementations of the Ruby language for

each platform

Page 15: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

RubyMotion

iOS SDK

Objective-C

Objective-C Runtime

iOS

Page 16: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

RubyMotion

Android SDK

Java

Dalvik / ART

JNI

Android

Page 17: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

iOS Hello Worldclass 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 endend

Page 18: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

iOS Hello Worldclass 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 endend

Page 19: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Android Hello Worldclass MainActivity < Android::App::Activity def onCreate(savedInstanceState) super

text = Android::Widget::TextView.new(self) text.text = 'Hello RubyMotion!' self.contentView = text endend

Page 20: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Android Hello Worldclass MainActivity < Android::App::Activity def onCreate(savedInstanceState) super

text = Android::Widget::TextView.new(self) text.text = 'Hello RubyMotion!' self.contentView = text endend

Page 21: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Static compilation

Page 22: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Co

mp

ilati

on

Ruby File

AST

LLVM IR

Assembly (ARM or Intel)

Runtime

Page 23: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

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

Page 24: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Command-line interface

Page 25: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion
Page 26: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Creating a new project$ motion create --template=ios Hello$ motion create —-template=android Hello$ motion create --template=osx Hello

Page 27: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Project configuration

$ vi Rakefile

Motion::Project::App.setup do |app| # Use `rake config' to see complete project settings. app.name = 'Hello'end

Page 28: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Running

$ rake$ rake simulator

$ rake$ rake emulator

iOS Simulator

Android Emulator

$ rake device

iOS/Android Device

Page 29: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Distribution

$ rake archive:distribution #=> foo.ipa

$ rake release #=> foo.apk

iOS App Store

Android Play Store

Page 30: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion
Page 31: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion
Page 32: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion
Page 33: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion
Page 34: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion
Page 35: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion
Page 36: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

https://www.jetbrains.com/ruby/rubymotion

Page 37: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

What’s RubyMotion?• Static (AOT) Ruby compiler

• 2 runtimes - implementations of Ruby

• Objective-C (iOS, OS X, tvOS, watchOS)

• Java (Android)

• Build system - based on Rake

• REPL

Page 38: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

RubyMotion Starter

Page 39: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Fully-featured (iOS and Android)

Page 40: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Splash screen

Page 41: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

rubymotion.com/download

Page 42: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

motion-game

Page 43: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Create games for iOS and Android

Page 44: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Cross-platform Ruby API to write games

Page 45: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Fully-featured

Scene Graph

Sensor Events

Sprites

Animations

Physics

Particles

Parallax

Networking

UI Widgets

Page 46: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion
Page 47: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

One code base ↓

iOS, tvOS and Android

Page 48: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

100% cross-platform

Page 49: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

$ gem install motion-game

Page 50: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Future of RubyMotion

Page 51: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

iOS Android

Language Ruby

Environment Your Editor + Terminal

APIs iOS SDK Android SDK

Page 52: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

iOS Android

Language Ruby

Environment Your Editor + Terminal

APIs iOS SDK Android SDK

Page 53: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

If you make a cross-platform app in RubyMotion:

1) Use platform-specific APIs for user-interface

2) Share common code

Page 54: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Need to learn 2 set of APIs

Page 55: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Need to maintain 2 different codebases

Page 56: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

iOS Android

Language Ruby

Environment Your Editor + Terminal

APIs iOS SDK Android SDK

Page 57: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

iOS Android

Language Ruby

Environment Your Editor + Terminal

APIs

iOS SDK Android SDK

Ruby Framework

Page 58: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Flow

Page 59: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Cross-platform framework for RubyMotion

Page 60: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Rails of RubyMotion

Page 61: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

One code base ↓

iOS and Android

Page 62: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Set of cross-platform libraries for RubyMotion

Page 63: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

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!)

Page 64: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Net - GET requestNet.get("https://httpbin.org/get?user_id=1") do |response| if response.status == 200 response.body['args']['user_id'] # 1 end end

Page 65: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Net - POST requestoptions = { 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 endend

Page 66: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Net - Host reachabilityservice = Net.reachable?("www.google.fr") do |reachable| if reachable # … endend

# …service.stop

Page 67: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

JSONJSON.load('{"foo":"bar"}') # => {"foo" => "bar"}

{"foo" => "bar"}.to_json # => '{"foo":"bar"}'

Page 68: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Digest# quickDigest::MD5.digest(‘hello’) #=> '5d41402abc4b2a76b9719d911017c592'

# longdigest = Digest::MD5.newdigest.update('hello')digest.digest #=> '5d41402abc4b2a76b9719d911017c592'

Page 69: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Base64Base64.encode('xx') #=> eHg=

Base64.decode('eHg=') #=> xx

Page 70: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

StoreStore['key'] = 42

Store[‘key'] #=> 42

Store.all #=> { 'hey' => 42 }

Store.delete('key')

Page 71: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Location - Monitorservice = 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 endend

# ...service.stop

Page 72: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Location - Reverse geocodeLocation.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 endend

Page 73: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Task - Timerstimer1 = Task.after 0.5 do # executed after half secondend

timer2 = Task.every 2.5 do # executed every two and half secondsend

# ...timer2.stop

Page 74: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Task - Main threadTask.main do # update UI stuff…end

Page 75: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Task - Background threadTask.background do # something that takes time…end

Page 76: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Task - Sequential queuesq = Task.queue

q.schedule do # job 1endq.schedule do # job 2, will be executed after job 1endq.schedule do # job 3, will be executed after job 2end

q.wait # wait for all jobs to finish

Page 77: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

UI

Page 78: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

UI library• Set of cross-platform classes for UI components

• Cross-platform layout system

Page 79: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

UI - Classes• UI::View

• UI::Button

• UI::TextInput

• UI::Label

• UI::Image

• UI::Table

• UI::Web

• UI::Color

• UI::Font

• UI::Alert

• UI::Screen

• UI::Navigation

• UI::Application

• …

Page 80: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

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

Page 81: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

UI - Layoutview.widthview.height

view.leftview.rightview.topview.bottom

Page 82: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

UI - Layoutview.padding_leftview.padding_rightview.padding_topview.padding_bottom

view.margin_leftview.margin_rightview.margin_topview.margin_bottom

view.border_leftview.border_rightview.border_topview.border_bottom

Page 83: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

UI - Layoutview.flex # 0 or 1view.direction # :inherit, :ltr, :rtlview.flex_direction # :column, :column_reverse, # :row, :row_reverseview.justify_content # :flex_start, :center, flex_end, # :space_between, :space_aroundview.align_content # :auto, :flex_start, :center, # :flex_end, :stretchview.align_items # :auto, :flex_start, :center, # :flex_end, :stretchview.align_self # :auto, :flex_start, :center, # :flex_end, :stretchview.position_type # :relative, :absoluteview.flex_wrap # :no_wrap, :wrap

Page 84: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Demo

Page 85: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Flow is completely open-source

Page 86: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

https://github.com/HipByte/Flow

Page 87: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Already used in production

Page 88: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Actively working on it

Page 89: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

First release later this month!

Page 90: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Flow will be included in RubyMotion 5.0 when

it’s fully complete

Page 91: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Please give it a try!

Page 92: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

What’s RubyMotion?• Static (AOT) Ruby compiler

• 2 runtimes - implementations of Ruby

• Objective-C (iOS, OS X, tvOS, watchOS)

• Java (Android)

• Build system - based on Rake

• REPL

Page 93: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

What’s RubyMotion?• Static (AOT) Ruby compiler

• 2 runtimes - implementations of Ruby

• Objective-C (iOS, OS X, tvOS, watchOS)

• Java (Android)

• Build system - based on Rake

• REPL

• Cross-platform framework

Page 94: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Recap

Page 95: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

RubyMotion is fun!

Page 96: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Please download RubyMotion!

Page 97: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

rubymotion.com/download

Page 98: [Srijan Wednesday Webinars] Building Full-Fledged Native Apps Using RubyMotion

Thank you!

www.rubymotion.com @rubymotion [email protected]

Questions?