introducing nativescript - tj vantoll · invoking native apis • v8/javascriptcore have c++...

47
Introducing NativeScript TJ VanToll | @tjvantoll

Upload: others

Post on 22-May-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Introducing NativeScript TJ VanToll | @tjvantoll

Page 2: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

nativescript.org

Page 3: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

NativeScript Timeline

•  0.9 •  Public Beta •  March 5th, 2015

•  1.0 •  Go-live license •  Windows Phone support •  May 2015

Page 4: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

What is NativeScript?

•  A runtime for building and running native iOS, Android, and Windows Phone apps with a single, JavaScript code base

Page 5: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

•  Bridge

Page 6: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

•  No DOM

•  No cross compilation

!=

!=

Page 7: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

NativeScript Android example

Output:

Page 8: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses
Page 9: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

NativeScript iOS example

Page 10: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses
Page 11: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

How does this work?

Page 12: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

NativeScript and JS VMs

•  NativeScript runs JavaScript on a JavaScript VM •  JavaScriptCore on iOS •  V8 on Android •  JavaScriptCore on Windows

Page 13: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

•  Runs on V8

•  Runs on JavaScriptCore

Page 14: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Gathering Native APIs

•  NativeScript uses reflection to build a list of available APIs for each platform. •  For optimal performance, this metadata is pre-generated, and injected into the app package at build time.

Page 15: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Injecting native APIs

•  V8/JavaScript Core have APIs to inject global variables

Page 16: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Invoking native APIs

•  V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. •  The NativeScript runtime uses those callbacks to translate JS calls into native calls. •  On iOS, you can directly call Objective-C APIs from C++ code. •  On Android, NativeScript uses Android’s JNI (Java Native Interface) to make the bridge from C++ to Java.

Page 17: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

•  1) The V8 function callback runs. •  2) The NativeScript runtime uses its metadata to know that Time() means it needs to instantiate an android.text.format.Time object. •  3) The NativeScript runtime uses the JNI to instantiate an android.text.format.Time object and keeps a reference to it.

Page 18: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

•  4) The NativeScript runtime returns a JS object that proxies the Java Time object. •  5) Control returns to JS where the proxy object gets stored as a local time variable.

Page 19: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses
Page 20: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

So do you only write native code?

No

Page 21: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

TNS modules

•  NativeScript-provided modules that provide cross-platform functionality. •  There are dozens of them and they’re easy to write yourself. •  TNS modules follow Node module’s conventions (CommonJS).

Page 22: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

TNS file module

Page 23: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

HTTP module example

Page 24: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Custom TNS modules

Page 25: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Using the custom device module

Page 26: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Community modules

•  https://github.com/alejonext/NativeNumber •  Someone created this 7 hours after the NativeScript public release.

Page 27: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

But how do I turn this into an app?

Page 28: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Two ways to use NativeScript

1)

2)

Page 29: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

•  Backend-as-a-service • Push notifications, cloud data, file storage, and more

•  Analytics •  AppBuilder

• Cloud builds (build iOS apps on Windows, Windows Phone apps on a Mac) • NativeScript debugging and tooling

•  Automated app testing •  And more!

http://telerik.com/platform

Page 30: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

https://www.telerik.com/purchase/platform

Page 31: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

NativeScript CLI

•  Free and open source •  https://github.com/nativescript/nativescript-cli

Page 32: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

NativeScript CLI requirements

•  https://github.com/nativescript/nativescript-cli#system-requirements

•  Xcode, Xcode CLI tools, iOS SDK

•  JDK, Apache Ant, Android SDK

Page 33: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Starting a new project

Page 34: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Running on iOS

Page 35: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Running on Android

Page 36: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses
Page 37: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

app.js

Page 38: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Pages •  XML markup structure •  Elements (e.g. <Page>, <Label>) are TNS modules

Page 39: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Data binding

Page 40: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Data binding improved

Page 41: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

CSS

Page 42: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

http://docs.nativescript.org/styling#supported-properties

Page 43: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Demo time!

Page 44: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Contribute! (nativescript.org/contribute)

Page 45: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Follow NativeScript

•  @nativescript •  https://nativescript.org/blog

Page 46: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Questions?

•  TJ VanToll | @tjvantoll

Page 47: Introducing NativeScript - TJ VanToll · Invoking native APIs • V8/JavaScriptCore have C++ callbacks for JS function calls and property accesses. • The NativeScript runtime uses

Thanks!