building cross-platform mobile apps

57
Building Cross-Platform Mobile Apps Mobile Dev+Test 14 April 2015 - San Diego CA

Upload: troy-miles

Post on 15-Jul-2015

317 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Building Cross-Platform Mobile Apps

Building Cross-Platform Mobile AppsMobile Dev+Test 14 April 2015 - San Diego CA

Page 2: Building Cross-Platform Mobile Apps

Troy Miles

Over 35 years of programming experience

Blog: http://therockncoder.blogspot.com/

Twitter: @therockncoder

Email: [email protected]

GitHub: https://github.com/Rockncoder

Page 3: Building Cross-Platform Mobile Apps

Agenda

A Quick & Dirty JavaScript Overview

What is PhoneGap?

Building PhoneGap Apps

Debugging

Plugins

Page 4: Building Cross-Platform Mobile Apps

AgendaBackbone

Underscore

Models

Collections

Views

The Router

Page 5: Building Cross-Platform Mobile Apps

Agenda

Templates

Chocolate Chip

Lists

Look and Feel

Summary

Page 6: Building Cross-Platform Mobile Apps

The End of HTML5 as a Platform?

Facebook mobile apps on iOS and Android were originally using HTML5

Users complained about speed and style

In 2012, Facebook switch to native apps

The pundits announced the end of HTML5 as a mobile platform and the end of PhoneGap too

Page 7: Building Cross-Platform Mobile Apps

JavaScript Best Practices

Avoid sloppy JavaScript

Avoid the Global Space

Encapsulate Code into Objects

Use Design Patterns

Page 8: Building Cross-Platform Mobile Apps

Avoid Sloppy JavaScript

Use “strict” mode

Always use ‘===’ & ‘!==’

Code in JavaScript not C#, Java, Ruby, etc.

Use JSLint or JSHint

Page 9: Building Cross-Platform Mobile Apps

Avoid the Global Space

Minimize use of global variables

Use Name-spacing

Use anonymous/immediate functions when appropriate

Page 10: Building Cross-Platform Mobile Apps

Functions

Functions are a first class type

Like other types they can be passed and assigned freely

Anonymous functions are used frequently

Page 11: Building Cross-Platform Mobile Apps

Objects

Objects are some what like Key/Value dictionaries in other languages

The Key can be anything when wrapped in quotes

The Value can be any type including a function

Page 12: Building Cross-Platform Mobile Apps

Events

Events allow for excellent separation of concerns

You can listen for system events or

Trigger and respond to your own

Many external libraries will communicate via events

Page 13: Building Cross-Platform Mobile Apps

Promises

A rather new JavaScript concept

Used to handle asynchronous callbacks

The app uses jQuery’s version

Page 14: Building Cross-Platform Mobile Apps

PhoneGap/Cordova History2009: 1st developed at an iPhoneDevCamp event

2009: Developers form a company, Nitobi

2011: Adobe acquires Nitobi

2011: Adobe open sources PhoneGap project to Apache

2012: Apache gives the project the name Cordova

Page 15: Building Cross-Platform Mobile Apps

Cordova ForksAdobe PhoneGap

IBM Worklight

Telerik Platform

Intel XDK

BlackBerry WebWorks

The Ionic Framework

Page 16: Building Cross-Platform Mobile Apps

Target PlatformsAmazon FireOS

Google Android

BlackBerry 10

Firefox OS

Apple iOS

Ubuntu Linux

Microsoft Windows Phone 8

Page 17: Building Cross-Platform Mobile Apps

Development PlatformsOS X

All except Windows Phone / Windows

Windows

All except iOS

Linux

All except Windows Phone and iOS

Page 18: Building Cross-Platform Mobile Apps

PhoneGap

Current release is 4.2

Node.js is a hard requirement since version 3.0

It is all command line instead of IDE

Recommend not upgrading your app to a new version right away

Page 19: Building Cross-Platform Mobile Apps

How PhoneGap works?

Most device APIs include an internal web browser

PhoneGap uses this internal web browser as its app canvas

It adds more features to the navigator via software which bridges the gap between the internal web and the device

Page 20: Building Cross-Platform Mobile Apps

The Hard Things

Installation

Knowing the difference between PhoneGap & Cordova

Deciding what to do past hello, world

Page 21: Building Cross-Platform Mobile Apps

Node Package Managernpm is very popular in the open source community

cross-platform

coding standard

storage site + discovery mechanism

delivery mechanism

used by phonegap/cordova

Page 22: Building Cross-Platform Mobile Apps

Let’s build an app, part 1a

Page 23: Building Cross-Platform Mobile Apps

Hello, world

cordova create hello com.rnc.hello Hello

cd hello

cordova platform add browser --usegit

cordova run browser

Page 24: Building Cross-Platform Mobile Apps

hooks

Allows you to execute code at defined points in the Cordova application build process

Example minify your JavaScript

Page 25: Building Cross-Platform Mobile Apps

platforms

One directory for each device framework you support

Because of the complexity involved in getting individual machines setup, we will demonstrate this but not actually work through it as an excercise

Page 26: Building Cross-Platform Mobile Apps

pluginsModular pieces of native code which give your app increased functionality

Familiar Plugins

console

device

dialogs

inappbrowser

Page 27: Building Cross-Platform Mobile Apps

wwwYour app's root directory

laid out as a set of sub-directories

css

img

js

index.html

Page 28: Building Cross-Platform Mobile Apps

config.xml

Defines a widget

Must be in root directory

Actually defined by the W3C

http://www.w3.org/TR/widgets/#configuration-document-0

Page 29: Building Cross-Platform Mobile Apps

Let’s build an app, part 1b

Page 30: Building Cross-Platform Mobile Apps

Adding a plugin

Two important pieces of information

how to install a plugin

how to make it work

http://plugins.cordova.io/#/package/org.apache.cordova.camera

cordova plugin add org.apache.cordova.camera

Page 31: Building Cross-Platform Mobile Apps

navigator.camera.getPicture(onSuccess, onFail, { quality: 50,

destinationType: Camera.DestinationType.DATA_URL});

function onSuccess(imageData) {

var image = document.getElementById('myImage');

image.src = "data:image/jpeg;base64," + imageData;}

function onFail(message) {

alert('Failed because: ' + message);}

Page 32: Building Cross-Platform Mobile Apps

Debugging

Using the browser while developing

Chrome remote for Android devices

Safari remote for iOS devices

Windows Phone winre

Page 33: Building Cross-Platform Mobile Apps

UI Options

Roll Your Own

jQuery / jQuery UI

jQuery Mobile

Bootstrap

Chocolate Chip UI

Page 34: Building Cross-Platform Mobile Apps

Framework Options

Roll Your Own

AngularJS

Knockout

BackboneJS

Page 35: Building Cross-Platform Mobile Apps

Underscore

A utility belt library for JavaScript

Excellent at manipulating objects and collections

About 6kb minified and compressed

Required for Backbone apps

Page 36: Building Cross-Platform Mobile Apps

Backbone

A MV* Framework

Note: There are no controllers hence no ‘C’

More lightweight than Angular, Ember, or Knockout

Requires jQuery and Underscore

Page 37: Building Cross-Platform Mobile Apps

Models

The base object in Backbone

Essentially a wrapper around a JavaScript object

Use get and set command to access properties

Page 38: Building Cross-Platform Mobile Apps

Collections

A collection of models

Can associate a URL with a collection

Backbone native support of RESTful API

Can also use third party API

Page 39: Building Cross-Platform Mobile Apps

Views

Backbone’s UI layer

Also does much of what a controller would do in typical MVC

Page 40: Building Cross-Platform Mobile Apps

The Router

The router controls application state

In a web site it would control what is in the URL bar

PhoneGap apps may lack a visible URL bar, but it still exists

Page 41: Building Cross-Platform Mobile Apps

Templates

Templates render markup to the DOM in a cookie cutter fashion

Especially good for render collections to a view

Make it easier to create single page apps

Page 42: Building Cross-Platform Mobile Apps

Chocolate Chip UI

A UI Framework akin to jQuery Mobile or even Bootstrap

Does a great job of impersonating iOS, Android, and Windows Phone 8

Page 43: Building Cross-Platform Mobile Apps

Lists

You will work a lot with lists in mobile apps

In CC, lists will have the look and feel of the device

Lists typically will need a bit of code to make them fully functional

Page 44: Building Cross-Platform Mobile Apps

Lists

Lists have classes which enhance their looks

Classes exists to indicate:

Navigation to another page

Navigation to a details page

Page 45: Building Cross-Platform Mobile Apps

Widgets

CC uses a combo of its own stuff with HTML5

For example the Range Slider is simply an HTML5 input type=range

But a switch is a combination of HTML, CSS3, and JavaScript

Page 46: Building Cross-Platform Mobile Apps

Look & Feel

Switching the look and feel is easy, just change CSS files

PhoneGap version 3+ automates the process

Page 47: Building Cross-Platform Mobile Apps

Look & Feel

iOS: chui-ios-3.8.4.min.css

Android 4+: chui-android-3.8.4.min.css

Windows Phone 8: chui-win-3.8.4.min.css

Page 48: Building Cross-Platform Mobile Apps

Look & Feel

PhoneGap’s merges folder

one directory for each supported device

Its contents will be copied and overwrite during the build command

Name all of the css files identically

Place in each appropriate folder

Page 49: Building Cross-Platform Mobile Apps

Let’s build an app, part 2

Page 50: Building Cross-Platform Mobile Apps

pg-twitter

cordova create pg-twitter com.rnc.pgtwitter pg-twitter

cd pg-twitter

cordova platform add browser --usegit

cordova run browser

Page 51: Building Cross-Platform Mobile Apps

merges directory

Must be created by hand

named after support platforms

contents of the merge directory will be written to the www during build

files/directories with the same name will be overwritten

Page 52: Building Cross-Platform Mobile Apps

Two Things Really Quick

PhoneGap Build

PhoneGap Developer App

Page 53: Building Cross-Platform Mobile Apps

Resources

http://phonegap.com/

http://cordova.apache.org/docs/en/4.0.0/index.html

http://cordova.apache.org/

http://backbonejs.org/

http://underscorejs.org/

Page 54: Building Cross-Platform Mobile Apps

Resources

http://momentjs.com/

http://plugins.cordova.io/#/

http://jquery.com/

http://chocolatechip-ui.com/index.html

http://www.raymondcamden.com/

Page 55: Building Cross-Platform Mobile Apps

The Rockncoder

http://therockncoder.blogspot.com

http://www.youtube.com/user/rockncoder

https://github.com/Rockncoder/pg-twitter

http://www.slideshare.net/rockncoder

Page 56: Building Cross-Platform Mobile Apps

Summary

Speed of development

Ease of development

Cross-platform by design

Page 57: Building Cross-Platform Mobile Apps

Please rate this talk!http://spkr8.com/t/44241