65

Creating and Distributing Mobile Web Applications with PhoneGap

Embed Size (px)

DESCRIPTION

An introduction to using PhoneGap to creative native applications using Sencha Touch

Citation preview

Page 1: Creating and Distributing Mobile Web Applications with PhoneGap
Page 2: Creating and Distributing Mobile Web Applications with PhoneGap

James Pearce Sr Director, Developer Relations @ jamespearce [email protected]

Page 3: Creating and Distributing Mobile Web Applications with PhoneGap

Creating and distributing mobile web applications

with PhoneGap

Page 4: Creating and Distributing Mobile Web Applications with PhoneGap

Web appsare the future

Page 5: Creating and Distributing Mobile Web Applications with PhoneGap

Native appsare the present

Page 6: Creating and Distributing Mobile Web Applications with PhoneGap

Native Web

Cross-platform ✘ ✔

Linkable ✘ ✔

Discoverable ✔ ✘

Device APIs ✔ ✘

Page 7: Creating and Distributing Mobile Web Applications with PhoneGap

Native Web Hybrid

Cross-platform ✘ ✔ ✔

Linkable ✘ ✔ ✔

Discoverable ✔ ✘ ✔

Device APIs ✔ ✘ ✔

Page 8: Creating and Distributing Mobile Web Applications with PhoneGap

What is a hybrid app?

Page 9: Creating and Distributing Mobile Web Applications with PhoneGap

UIWebViewWebView

A simple hybrid application

HTMLCSS

JSStores

Page 10: Creating and Distributing Mobile Web Applications with PhoneGap

PhoneGaphttp://phonegap.com

MIT

Page 11: Creating and Distributing Mobile Web Applications with PhoneGap

+

Page 12: Creating and Distributing Mobile Web Applications with PhoneGap

UIWebViewWebView

A PhoneGap application

HTMLCSS

JSStores

JS APIs

Page 13: Creating and Distributing Mobile Web Applications with PhoneGap

PhoneGap APIsAccelerometerTap into the device’s motion sensor.

CameraCapture a photo using the device's camera.

CompassObtain the direction that the device is pointing.

ContactsWork with the devices contact database.

DeviceGather device specific information.

EventsHook into native events through JavaScript.

FileHook into native file system through JavaScript.

GeolocationMake your application location aware.

MediaRecord and play back audio files.

NetworkQuickly check the network state.

NotificationVisual, audible, and tactile device notifications.

StorageHook into the device’s native storage options.

Page 14: Creating and Distributing Mobile Web Applications with PhoneGap

Getting Startedwith iOS

Page 15: Creating and Distributing Mobile Web Applications with PhoneGap
Page 16: Creating and Distributing Mobile Web Applications with PhoneGap

http://phonegap.com

Page 17: Creating and Distributing Mobile Web Applications with PhoneGap
Page 18: Creating and Distributing Mobile Web Applications with PhoneGap

$> ./xcode4.sh seattlebars .

http://sencha.com/x/b9

Page 19: Creating and Distributing Mobile Web Applications with PhoneGap
Page 20: Creating and Distributing Mobile Web Applications with PhoneGap
Page 21: Creating and Distributing Mobile Web Applications with PhoneGap
Page 22: Creating and Distributing Mobile Web Applications with PhoneGap
Page 23: Creating and Distributing Mobile Web Applications with PhoneGap

@implementation PhoneGapDelegate...+ (NSString*) wwwFolderName {! return @"www";}+ (NSString*) startPage {! return @"index.html";}...NSString* startPage = [[self class] startPage];NSURL *appURL = [NSURL URLWithString:startPage];if(![appURL scheme]) { appURL = [NSURL fileURLWithPath:[[self class] pathForResource:startPage]];}...NSURLRequest *appReq = [NSURLRequest requestWithURL:appURL ...];[webView loadRequest:appReq];

How it works

Page 24: Creating and Distributing Mobile Web Applications with PhoneGap

Getting Startedwith Android

Page 25: Creating and Distributing Mobile Web Applications with PhoneGap
Page 26: Creating and Distributing Mobile Web Applications with PhoneGap
Page 27: Creating and Distributing Mobile Web Applications with PhoneGap
Page 28: Creating and Distributing Mobile Web Applications with PhoneGap
Page 29: Creating and Distributing Mobile Web Applications with PhoneGap

package com.phonegap.exampleapp;

import android.os.Bundle;import com.phonegap.*;

public class exampleapp extends DroidGap { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.loadUrl("file:///android_asset/www/index.html"); }}

How it works

Page 30: Creating and Distributing Mobile Web Applications with PhoneGap

Using aSencha Touch

Application

Page 31: Creating and Distributing Mobile Web Applications with PhoneGap

$> rm -r www $> ln -s myapp www

Page 32: Creating and Distributing Mobile Web Applications with PhoneGap

Don’t symlink theentire SDK, thanks

Don’t symlink yoursource control

Page 33: Creating and Distributing Mobile Web Applications with PhoneGap
Page 34: Creating and Distributing Mobile Web Applications with PhoneGap
Page 35: Creating and Distributing Mobile Web Applications with PhoneGap

NB these are now in the symlink dir

Page 36: Creating and Distributing Mobile Web Applications with PhoneGap
Page 37: Creating and Distributing Mobile Web Applications with PhoneGap

Ext.setup({ tabletStartupScreen: 'tablet_startup.png', phoneStartupScreen: 'phone_startup.png', icon: 'icon.png', glossOnIcon: false, ...

Page 38: Creating and Distributing Mobile Web Applications with PhoneGap
Page 39: Creating and Distributing Mobile Web Applications with PhoneGap

<aside>

</aside>Pacifist

+ =

Page 40: Creating and Distributing Mobile Web Applications with PhoneGap

$> rm -r assets/www $> ln -s myapp assets/www

Page 41: Creating and Distributing Mobile Web Applications with PhoneGap
Page 42: Creating and Distributing Mobile Web Applications with PhoneGap
Page 43: Creating and Distributing Mobile Web Applications with PhoneGap
Page 44: Creating and Distributing Mobile Web Applications with PhoneGap

So we’re done, right?

Page 45: Creating and Distributing Mobile Web Applications with PhoneGap

No.

Page 46: Creating and Distributing Mobile Web Applications with PhoneGap

UsingDevice APIs

Page 47: Creating and Distributing Mobile Web Applications with PhoneGap

AccelerometerTap into the device’s motion sensor.

CameraCapture a photo using the device's camera.

CompassObtain the direction that the device is pointing.

ContactsWork with the devices contact database.

DeviceGather device specific information.

EventsHook into native events through JavaScript.

FileHook into native file system through JavaScript.

GeolocationMake your application location aware.

MediaRecord and play back audio files.

NetworkQuickly check the network state.

NotificationVisual, audible, and tactile device notifications.

StorageHook into the device’s native storage options.

Page 48: Creating and Distributing Mobile Web Applications with PhoneGap

That’s what this was for

Page 49: Creating and Distributing Mobile Web Applications with PhoneGap

but...

Page 50: Creating and Distributing Mobile Web Applications with PhoneGap

Androidphonegap.js

iOSphonegap.js

Page 51: Creating and Distributing Mobile Web Applications with PhoneGap

package com.phonegap.exampleapp;

import android.os.Bundle;import com.phonegap.*;

public class exampleapp extends DroidGap { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.loadUrl("file:///android_asset/www/index_a.html"); }}

Alter entry points

Page 52: Creating and Distributing Mobile Web Applications with PhoneGap

<!doctype html><html> <head> <script type="text/javascript" src="app/app.js"> </script> <script type="text/javascript" src="phonegap.0.9.5.js"> </script> ...

index.html

Page 53: Creating and Distributing Mobile Web Applications with PhoneGap

<!doctype html><html> <head> <script type="text/javascript" src="app/app.js"> </script> <script type="text/javascript" src="phonegap_a.0.9.5.js"> </script> ...

index_a.html

Page 54: Creating and Distributing Mobile Web Applications with PhoneGap

deviceready event

document.addEventListener( "deviceready", onDeviceReady, false);

function onDeviceReady() {    // Now safe to use the PhoneGap API}

Page 55: Creating and Distributing Mobile Web Applications with PhoneGap

Avoiding race conditionsdocument.addEventListener( "deviceready", app.mainLaunch, false);

Ext.regApplication({ name: "app", launch: function() { this.launched = true; this.mainLaunch(); }, mainLaunch: function() { if (!device || !this.launched) {return;} ... }});

Page 56: Creating and Distributing Mobile Web Applications with PhoneGap

http://docs.phonegap.com

Page 57: Creating and Distributing Mobile Web Applications with PhoneGap

Cameranavigator.camera.getPicture(

function(imageData) {    var img = document.getElementById('img');    img.src = "data:image/jpeg;base64," + imageData; },

function (message) {    alert('Failed because: ' + message); },

{ quality: 50 }

);

Page 58: Creating and Distributing Mobile Web Applications with PhoneGap

File I/O

var paths = navigator.fileMgr.getRootPaths();var file = paths[0] + "write.txt";

var writer = new FileWriter(file);writer.onwrite = function () {};writer.write("Hello Croatia!");

var reader = new FileReader();reader.onload = function (e) { alert(e.target.result);};reader.readAsText(file);

Page 59: Creating and Distributing Mobile Web Applications with PhoneGap

Contacts

navigator.service.contacts.create( { "displayName": "James Pearce", "organizations": ["Sencha"] }).save();

Page 60: Creating and Distributing Mobile Web Applications with PhoneGap

Contacts Demo

http://vimeo.com/23358554

Page 61: Creating and Distributing Mobile Web Applications with PhoneGap

Final thoughts

Page 62: Creating and Distributing Mobile Web Applications with PhoneGap

Plugins

Page 63: Creating and Distributing Mobile Web Applications with PhoneGap

http://pmuellr.github.com/weinre/

Page 64: Creating and Distributing Mobile Web Applications with PhoneGap

Hybrid appsXcode + EclipseNative wrapper

Native APIs

Page 65: Creating and Distributing Mobile Web Applications with PhoneGap

James Pearce Sr Director, Developer Relations @ jamespearce [email protected]