80
Introduction to PhoneGap Kuro Hsu @ JSDC 2012/05/19 & Building mobile apps with HTML5, CSS3 and Javascript.

[JSDC.tw] Introduction to PhoneGap

Embed Size (px)

Citation preview

Page 1: [JSDC.tw] Introduction to PhoneGap

Introduction to PhoneGap

Kuro Hsu @ JSDC

2012/05/19

& Building mobile apps with HTML5, CSS3 and Javascript.

Page 2: [JSDC.tw] Introduction to PhoneGap

Mobile is Growing

• In January 2012, 8.49 percent of website hits/page-views come from a handheld mobile device

-- StatCounter

• Mobile will be bigger than desktop internet in 5 years -- Morgan Stanley

• 2.1 billion mobile devices will have HTML5 browsers by 2016 up from 109 million in 2010 -- ABI Research

Page 3: [JSDC.tw] Introduction to PhoneGap
Page 4: [JSDC.tw] Introduction to PhoneGap

Mobile development is mess.

Android Java

BlackBerry Java

iOS Objective-C

Palm OS C, C++, Pascal

Symbian C++

Windows Phone C#

Page 5: [JSDC.tw] Introduction to PhoneGap

Mobile Users prefer browsers over apps

Page 6: [JSDC.tw] Introduction to PhoneGap
Page 7: [JSDC.tw] Introduction to PhoneGap

The Web is awesome!

http://mobilehtml5.org/

Page 8: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Setup the Viewport

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" /> @viewport { width: device-width; zoom: 1.0; maximum-scale: 1.0; user-zoom: fixed; /* zoom = 1, fixed = 0 */ }

Page 9: [JSDC.tw] Introduction to PhoneGap
Page 10: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Handheld Friendly

<meta name="handheldfriendly" content="true" />

• Mobile Optimized

<meta name= "mobileoptimized" content="320" />

Page 11: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Mobile CSS Reset

http://www.vcarrer.com/2010/11/css-mobile-reset.html

• CSS Features -webkit-text-size-adjust: none;

-webkit-tap-highlight-color: transparent; position: fixed; /* iOS5 & Android 2.2+ */ overflow: scroll; /* iOS5 */ ……, and more.

Page 12: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Media Queries

@media only screen and (max-width: 480px) { /* small screen styles */ } @media only screen and (min-width: 481px) { /* large screen styles */ }

Page 13: [JSDC.tw] Introduction to PhoneGap

http://mediaqueri.es

Page 14: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

<input type="number" pattern="[0-9]*" /> <input type="email" required /> <input type="text" autocapitalize="off" autocorrect="off" />

<input type="url" /> <input type="tel" /> <input type="date"> <input type="datetime"> <input type="month"> <input type="time"> <input type="range">

• Advanced Forms in Mobile

Page 15: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Advanced Forms in Mobile

Page 16: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Advanced links

<a href="tel:+886987654321"> Call Me</a> <a href="sms:+886987654321"> Send SMS</a>

Page 17: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Media Support

<video controls width="640" height="368" x-webkit-airplay="allow" src="content/side_with_the_seeds.mov"> </video> <audio controls preload="auto" autobuffer> <source src="elvis.mp3" /> <source src="elvis.ogg" /> </audio>

Page 18: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Events - Orientation:

window.addEventListener('orientationchange', chkOrientation, false); function chkOrientation(ev){

var orient; if (window.orientation) { orient = (Math.abs(window.orientation) === 90) ? 'landscape' : 'portrait'; }

}

Page 19: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Events - Touch events:

window.addEventListener("touchstart", touchEvents, false); window.addEventListener("touchmove", touchEvents, false); window.addEventListener("touchend", touchEvents, false); window.addEventListener("touchcancel", touchEvents, false); function touchEvents(ev){ // do something... }

Page 20: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Events – Gesture events : window.addEventListener("gesturestart", gstEvents, false); window.addEventListener("gestureend", gstEvents, false); window.addEventListener("gesturechange", gstEvents, false); function gstEvents(ev){ // do something... }

Page 21: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Events - DeviceMotion:

window.addEventListener("devicemotion", accel, false); function accel(ev){ // ev.acceleration.x/y/z; // do something… }

Page 22: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Geolocation API

if(window.navigator.geolocation){ var geo = navigatoe.geolocation(); geo.getCurrentPosition(getPosSuccess); } function getPosSuccess(pos){ var lat = pos.coords.latitude; var lon = pos.coords.longitude; }

window.navigator.geolocation.getCurrentPosition( success, [failure, [options]] );

Page 23: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Canvas API

Page 24: [JSDC.tw] Introduction to PhoneGap
Page 25: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• Offline Web Application Cache

<html manifest="/cache.manifest">

# COMMENT THERE CACHE MANIFEST js/* index.html style.css ……

NETWORK: search.php …… FALLBACK: dynamic.php …… CACHE: ……

Page 26: [JSDC.tw] Introduction to PhoneGap

Build Mobile Web with HTML5

• LocalStorage / SessionStorage

localStorage["bar"] = "hello world!"; var foo = localStorage.getItem("bar"); localStorage.setItem("bar", "hello JSDC!"); localStorage.removeItem("bar"); localStorage.clear();

Page 27: [JSDC.tw] Introduction to PhoneGap

Mobile Web Strategy

Page 28: [JSDC.tw] Introduction to PhoneGap

Mobile Web Strategy

• Evaluate your requirement

Page 29: [JSDC.tw] Introduction to PhoneGap

Mobile Web Strategy

• Mobile-First, Responsive Design

Page 30: [JSDC.tw] Introduction to PhoneGap

Mobile Web Strategy

• Progressive Enhancement

Source: Adam Wang

Page 31: [JSDC.tw] Introduction to PhoneGap

Mobile Web Strategy

• Lighter is better

Page 32: [JSDC.tw] Introduction to PhoneGap

Mobile Web Strategy

• Evaluate your requirement

• Mobile-First, Responsive Design

• Progressive Enhancement

• Lighter is better

Page 33: [JSDC.tw] Introduction to PhoneGap

To App or Not to App?

Page 34: [JSDC.tw] Introduction to PhoneGap
Page 35: [JSDC.tw] Introduction to PhoneGap
Page 36: [JSDC.tw] Introduction to PhoneGap

Web vs. Native

Web Native

Dev Cost Reasonable Expensive

Dev Time Short Long

Portability High None

Performance Fast Very Fast

Native Functionality No All

App Store Distribution No Yes

Extensible No Yes

Page 37: [JSDC.tw] Introduction to PhoneGap

Web vs. Hybrid vs. Native

Web Hybrid Native

Dev Cost Reasonable Reasonable Expensive

Dev Time Short Short Long

Portability High High None

Performance Fast Fast Very Fast

Native Functionality No Yes All

App Store Distribution No Yes Yes

Extensible No Yes Yes

Page 38: [JSDC.tw] Introduction to PhoneGap

PhoneGap

Page 39: [JSDC.tw] Introduction to PhoneGap

PhoneGap

Started in 2008

iPhoneDevCamp

Working prototype with geolocation

Page 40: [JSDC.tw] Introduction to PhoneGap

PhoneGap

2008: support iPhone, Android and Blackberry 4

2009: Added Symbian and webOS support. "Rejected" by Apple.

2011: Added Windows Phone 7 support. Adobe officially announced the acquisition of Nitobi Software on October 4. In process to becoming a project under the Apache

Software Foundation. And New Name…

Page 41: [JSDC.tw] Introduction to PhoneGap

Callback (PhoneGap 1.1.0)

Page 42: [JSDC.tw] Introduction to PhoneGap

Callback

Page 43: [JSDC.tw] Introduction to PhoneGap

Apache Cordova ( From PhoneGap 1.5.0 )

Page 44: [JSDC.tw] Introduction to PhoneGap

Apache Cordova

Cordova is the open source project

PhoneGap is the implementation

Like Webkit for Chrome/Safari

http://incubator.apache.org/cordova/

Page 45: [JSDC.tw] Introduction to PhoneGap

What is PhoneGap ?

Page 46: [JSDC.tw] Introduction to PhoneGap

Basically just a webkit browser with all the chrome removed, even the menu bar, and dose everything a browser does.

What is PhoneGap / Cordova ?

Page 47: [JSDC.tw] Introduction to PhoneGap

Basically just a webkit browser with all the chrome removed, even the menu bar, and dose everything a browser does.

What is PhoneGap / Cordova ?

Page 48: [JSDC.tw] Introduction to PhoneGap

PhoneGap is an HTML5 app platform that allows you to author native applications with web technologies and get access to APIs and app stores. PhoneGap leverages web technologies developers already know best... HTML and JavaScript.

What is PhoneGap / Cordova ?

Page 49: [JSDC.tw] Introduction to PhoneGap

Write Once, Run Anywhere?

So, it means...?

Page 50: [JSDC.tw] Introduction to PhoneGap

NO

Page 51: [JSDC.tw] Introduction to PhoneGap

Write Once, Debug Everywhere.

The Truth Is...

Page 52: [JSDC.tw] Introduction to PhoneGap
Page 53: [JSDC.tw] Introduction to PhoneGap

Uses Platforms Native Control

iOS Android Blackberry webOS Symbian Windows Phone 7.0

WebKit 532.9

WebCore Javascript

Core

WebKit with V8

WebKit WebKit

with Piranah

WebKit S60 or Qt?

Page 54: [JSDC.tw] Introduction to PhoneGap

Uses Platforms Native Control

iOS Android Blackberry webOS Symbian Windows Phone 7.0

WebKit 532.9

WebCore Javascript

Core

WebKit with V8

WebKit WebKit

with Piranah

WebKit S60 or Qt? IE7

Page 55: [JSDC.tw] Introduction to PhoneGap

Uses Platforms Native Control

iOS Android Blackberry webOS Symbian Windows Phone 7.5

WebKit 532.9

WebCore Javascript

Core

WebKit with V8

WebKit WebKit

with Piranah

WebKit S60 or Qt? IE9

Page 56: [JSDC.tw] Introduction to PhoneGap

Uses Platforms Native Control

iOS Android Blackberry webOS Symbian Windows Phone 7.5

WebKit 532.9

WebCore Javascript

Core

WebKit with V8

WebKit WebKit

with Piranah

WebKit S60 or Qt? IE9

Page 57: [JSDC.tw] Introduction to PhoneGap
Page 58: [JSDC.tw] Introduction to PhoneGap

1. Write a web app using HTML5 technologies: HTML5 / CSS / JS

How does PhoneGap work?

Page 59: [JSDC.tw] Introduction to PhoneGap

2. Package your web app into PhoneGap

How does PhoneGap work?

Page 60: [JSDC.tw] Introduction to PhoneGap

2. Package your web app into PhoneGap

How does PhoneGap work?

Page 61: [JSDC.tw] Introduction to PhoneGap

3. Deploy your Native App to multiple devices ( iOS, Android, Blackberry, WP 7..., and so on. )

How does PhoneGap work?

Page 62: [JSDC.tw] Introduction to PhoneGap

Native Web Control

How does PhoneGap work?

FFI

Common Plugins

Custom Plugins

* FFI = Foreign Function Interface

Page 63: [JSDC.tw] Introduction to PhoneGap

Common Plugins: Good support across platforms.

PhoneGap API / Plugins:

Accelerometer

Camera

Capture

Compass

Connection

Contacts

Device

Events

File

Geolocation

Media

Notification

Storage

Page 64: [JSDC.tw] Introduction to PhoneGap

PhoneGap 1.7.0 - http://phonegap.com/about/features

Page 65: [JSDC.tw] Introduction to PhoneGap

Custom Plugins: Terrible support across platforms, and you can make it on you own!

PhoneGap API / Plugins:

https://github.com/phonegap/phonegap-plugins

Page 66: [JSDC.tw] Introduction to PhoneGap

An interface to the most common set of device functionality.

All APIs features are plugins.

All accessible through JavaScript (PhoneGap Bridge).

PhoneGap API / Plugins:

Page 67: [JSDC.tw] Introduction to PhoneGap

JDK 1.6+

Android SDK with an Android 2.2+

Eclipse & Android ADT plugin for Eclipse OR Any Text Editor

PhoneGap

Requirements ( for Android )

Page 68: [JSDC.tw] Introduction to PhoneGap

Get Started !

http://phonegap.com/start

Page 69: [JSDC.tw] Introduction to PhoneGap

Mobile Debugging

Page 70: [JSDC.tw] Introduction to PhoneGap

Webkit on PC

Mobile Debugging

Page 71: [JSDC.tw] Introduction to PhoneGap

Mobile Safari debugging

Mobile Debugging

Page 72: [JSDC.tw] Introduction to PhoneGap

Ripple Mobile Environment Emulator (Chrome)

Mobile Debugging

Page 73: [JSDC.tw] Introduction to PhoneGap

Weinre / debug.phonegap.com

Mobile Debugging

Page 74: [JSDC.tw] Introduction to PhoneGap

Adobe Shadow

Mobile Debugging

Page 75: [JSDC.tw] Introduction to PhoneGap

Webkit on PC

Mobile Safari debugging

Ripple Mobile Environment Emulator

weinre ( WEb INspector REmote )

Adobe Shadow

Mobile Debugging

Page 76: [JSDC.tw] Introduction to PhoneGap

PhoneGap doesn't bundle a UI framework, but they support any JS framework that works in the browser.

PhoneGap is just a fancy browser.

Native still gives the best performance.

Page 77: [JSDC.tw] Introduction to PhoneGap

“The ultimate purpose of PhoneGap is to cease to exist…”

- Brian LeRoux, Nitobi (now Adobe)

http://phonegap.com/2012/05/09/phonegap-beliefs-goals-and-philosophy/

Page 78: [JSDC.tw] Introduction to PhoneGap

Web is Native.

Page 79: [JSDC.tw] Introduction to PhoneGap

JavaScript Rocks !

Page 80: [JSDC.tw] Introduction to PhoneGap

Thanks!

Kuro Hsu

kurotanshi @ gmail.com

http://kuro.tw

https://twitter.com/kurotanshi

http://www.plurk.com/kurotanshi