introduction to phonegap

15
Intro to PhoneGap Ramesh Nair / hiddentao Oct 2014 @ Taipei Javascript Enthusiasts

Upload: ramesh-nair

Post on 02-Dec-2014

293 views

Category:

Software


2 download

DESCRIPTION

A brief introduction to PhoneGap and getting started building hybrid mobile apps.

TRANSCRIPT

Page 1: Introduction to PhoneGap

Intro to PhoneGap

Ramesh Nair / hiddentao

Oct 2014 @ Taipei Javascript Enthusiasts

Page 2: Introduction to PhoneGap

What is PhoneGap?• Build mobile apps using HTML5, Javascript and CSS

• Created by Nitobi in 2008

• Acquired by Adobe in 2011

• Code also given to Apache to create “Cordova”

• Today 2 projects: Phonegap and Cordova

• If Cordova = Blink/Webkit then Phonegap = Chrome

• PhoneGap adds extra features to Cordova (e.g. cloud build)

• http://cordova.apache.org/

• http://phonegap.com/

Page 3: Introduction to PhoneGap

Single codebase Multiple platforms

img: http://adrocknaphobia.github.io/preso-future-of-mobile/

Page 4: Introduction to PhoneGap

img: http://chelramsey.com/phonegap-vs-native-builds-a-12-month-review/

Page 5: Introduction to PhoneGap

How does it work?

HTML, Javascript, CSS

PhoneGap Javascript API

Web browser / UIWebView

PhoneGap Native Library

Native Platform

Mobile app

Page 6: Introduction to PhoneGap

Getting started

$ npm install -g cordova

$ cordova create ./mydemo com.mydemo MyDemo

$ cd mydemo

$ cordova platform add android

$ cordova run android

Java-like package

name

Assume iOS/Android

SDK is setup

Pre-requisites: Android SDK, ANDROID_HOME, ant

Page 7: Introduction to PhoneGap

Folder layout

config.xml - used to generate iOS/Android config

hooks/ - actions to run at various points during the build

platforms/ - apps for each platform, same as normal apps

plugins/ - cordova/phonegap plugins which add functionality

www/ - where your HTML, JS and CSS live-> platforms/android/assets/www-> platforms/ios/www-> …

Page 8: Introduction to PhoneGap

Live-reload = rapid feedback• “The PhoneGap Developer App”

• http://app.phonegap.com/

• Available from iOS and Android app stores

• phonegap serve

• npm install -g phonegap

• Watches www/ folder for changes, tells page to reload itself

• You can test this in a normal browser too

• There is also cordova serve but not sure what it’s supposed to do

Page 9: Introduction to PhoneGap

Plugins• Allow you to interact with the rest of your device (beyond the web

browser) through Javascript

• Plugins usually have 2 components:

1. Platform-specific native layer (Android, iOS, etc)

2. Javascript API (used by your app)

• All standard device features (e.g. contacts, compass, etc) accessed through plugins

• Can also interact with other apps (e.g. Facebook app, Google Maps)

• Plugin registry: http://plugins.cordova.io/

Page 10: Introduction to PhoneGap

Adding a plugin• Geolocation plugin (lat/long)

• http://plugins.cordova.io/#/package/org.apache.cordova.geolocation

• We use navigator.geolocation.watchPosition to get the latitude/longitude co-ordinates.

$ cordova plugin add org.apache.cordova.geolocation

$ cordova run android …OR… phonegap serve

Page 11: Introduction to PhoneGap

Live-reload issues• PhoneGap Developer App comes with basic plugins -

geolocation, camera, etc.

• But what if we want to use a plugin it doesn’t have, e.g. Google Maps or our own custom plugin?

• We can’t use live-reload with phonegap serve…

• …so we will build the live reload mechanism ourselves!

Page 12: Introduction to PhoneGap

Live-reload issues• PhoneGap Developer App comes with basic plugins -

geolocation, camera, etc.

• But what if we want to use a plugin it doesn’t have, e.g. Google Maps or our own custom plugin?

• We can’t use live-reload with phonegap serve…

• …so we will build the live reload mechanism ourselves!

Page 13: Introduction to PhoneGap

Use gulp for live-reload• gulp-server-livereload - watch for changes and

send reload signal

• gulp-replace-task - auto-insert live reload host:port into HTML page

• Demo app

• https://github.com/hiddentao/phonegap-demo-app

Page 14: Introduction to PhoneGap

Useful links• Ionic framework - http://ionicframework.com

• Like Twitter Bootstrap but for hybrid apps.

• Built on Angular.JS

• Meteor - http://meteor.com/

• Isomorphic Javascript framework (built on node.js)

• Lets you push code update to clients on-the-fly

• PhoneGap build - https://build.phonegap.com/

• Build and packaging done on a cloud server, saves you time and effort

• Trigger.io - https://trigger.io/

• Like PhoneGap but is proprietary and costs money

• Easier to use than PhoneGap, better plugin integration, etc.

Page 15: Introduction to PhoneGap

Any Questions?

“PhoneGap is making the world a better place through constructing elegant hierarchies for maximum

code reuse and extensibility”