common phonegap gotchas (#pgday eu 2016)

47
COMMON PHONEGAP GOTCHAS PHONEGAP DAY EU 2016 YankoPayanko (pixabay)

Upload: kerri-shotts

Post on 20-Feb-2017

794 views

Category:

Presentations & Public Speaking


3 download

TRANSCRIPT

Page 1: Common PhoneGap Gotchas (#PGDay EU 2016)

COMMON PHONEGAP GOTCHASPHONEGAP DAY EU 2016

YankoPayanko (pixabay)

Page 2: Common PhoneGap Gotchas (#PGDay EU 2016)

HELLO!MY NAME IS KERRI

Photo by Bill Bertram

Page 3: Common PhoneGap Gotchas (#PGDay EU 2016)

PURPOSE • IDENTIFY PAIN POINTS

• LEARN THE "EASY" WAY

• HAPPIER USERS

• KEEP BRAIN CELLS

Page 4: Common PhoneGap Gotchas (#PGDay EU 2016)

COMMON PAIN POINTS*

• PLUGIN ISSUES

• XHR / AJAX FAILURES / CSP / CORS

• CLI INSTALLATION

• ICONS / LAUNCH SCREENS

• TREATING PHONEGAP AS A BROWSER / SERVER

• MISUNDERSTANDING PHONEGAP TOOLS

• DATA STORAGE

• RESPONSIVE DESIGN

• SLOW

• DIDN'T EXPECT PLATFORM QUIRKS*NOT SCIENTIFICALLY DETERMINED

Page 5: Common PhoneGap Gotchas (#PGDay EU 2016)

CLI INSTALLATION

• WINDOWS SEEMS TO BE PROBLEMATIC FOR USERS

• NETWORK RESTRICTIONS / PROXIES GET IN THE WAY

• OFTEN HAVE DIFFICULTY CONFIGURING ENVIRONMENT VARIABLES CORRECTLY

• SOMETIMES SECURITY PROGRAMS CAUSE INSTALL PROBLEMS

• NPM WARNINGS LOOK SCARY

• SOME GRUMBLING ABOUT NUMBER OF PREREQUISITES

Page 6: Common PhoneGap Gotchas (#PGDay EU 2016)

NOT A BROWSER

• DON'T WRAP A WEBSITE

• KEEP CODE LOCAL

Page 7: Common PhoneGap Gotchas (#PGDay EU 2016)

BrowserWeb View

HTML

CSS

JS

PhoneGapWeb View

HTML

CSS

JS

Page 8: Common PhoneGap Gotchas (#PGDay EU 2016)

Browser PhoneGapWeb View

HTML

CSS

JS

Native Code

Core

Plugins

Bridge

Device

Contacts

Storage

Sharing

IAP

SDKs

•••

Interfaces with

Browser Chrome

User InterfaceVisible Security; Download UI

Web View

HTML

CSS

JS

Device

GPS

Accelerometer

Limited Storage

•••

Device

Interfaces With

Bookmarks

Non-sandboxed

Storage•••

Interfaces With

Browser PhoneGapWeb View

HTML

CSS

JS

Native Code

Core

Plugins

Bridge

Device

Contacts

Storage

Sharing

IAP

SDKs

•••

Interfaces with

Browser Chrome

User InterfaceVisible Security; Download UI

Web View

HTML

CSS

JS

Device

GPS

Accelerometer

Limited Storage

•••

Device

Interfaces With

Bookmarks

Non-sandboxed

Storage•••

Interfaces With

Page 9: Common PhoneGap Gotchas (#PGDay EU 2016)

SINGLE PAGE APP (SPA)

• DYNAMIC DOM

• EASIER ANIMATION

• EASIER APP STATE

• FASTER

Page 10: Common PhoneGap Gotchas (#PGDay EU 2016)

NOT A SERVER

Page 11: Common PhoneGap Gotchas (#PGDay EU 2016)

Server

PHP

function consol}

Node

function consol}

.NET

function consol}

etc.

function consol}

MySQL SQL Server

SQLite etc.

PhoneGap

JS

function consol}

CSS

nav { dispp z-ind}

HTML

<nav id= <a hr</nav>

SQLite

IndexedDB

Page 12: Common PhoneGap Gotchas (#PGDay EU 2016)

Web ServerDatabase Server

PHP

function consol}

Node

function consol}

.NET

function consol}

etc.

function consol}

MySQL SQL Server

SQLite etc.

Mobile Device

PhoneGapApp

Fi

rew

all

XMLHttpRequest

Page 13: Common PhoneGap Gotchas (#PGDay EU 2016)

CLI, DESKTOP, DEVELOPER, BUILD, OH MY!

• ONE OF THE FIRST QUESTIONS I ASK: WHICH TOOL ARE YOU USING?

• DEVELOPER DOESN'T SUPPORT EVERY PLUGIN

• BUILD HAS DIFFERENT STRUCTURE THAN CLI

• DESKTOP'S TEMPLATES NEED UPDATED

Page 14: Common PhoneGap Gotchas (#PGDay EU 2016)

CLI Desktop App Developer App Build

What

Command line interface for creating, building,

managing, and running PhoneGap apps

Graphical user interface for creating, managing,

and quickly testing PhoneGap apps

Mobile app that connects to the Desktop app or `phonegap serve`

for rapid iteration purposes

Cloud service that builds apps for you without

needing a local development environment

Benefits Complete control Quickly create, manage, and serve apps

Rapid iteration with quick reload

Don't need a local dev environment

Upload from CLI, .zip, GitHub

Easy distribution

DisadvantagesNeed to install prereqs &

SDKS More maintenance

Template is out-of-date Have to use CLI to add

platforms/plugins Connection difficulties

Only core + select plugins

lots of edge cases not present in built apps

Connection difficulties

Different project structure

PGB vs NPM plugin repo

Page 15: Common PhoneGap Gotchas (#PGDay EU 2016)

PUT A PIN IN IT

Page 16: Common PhoneGap Gotchas (#PGDay EU 2016)

Platform

$ cordova platform add [email protected] --save

(config.xml)<engine name="ios" spec="4.1.0" />

Plugins

$ cordova plugin add [email protected] --save

(config.xml) <plugin name = "cordova-plugin-device" spec = "1.1.1" />

PhoneGap Build CLI Version

<preference name = "phonegap-version" value = "cli-6.0.0"/>

Page 17: Common PhoneGap Gotchas (#PGDay EU 2016)

PLUGIN ISSUES

• NOT INSTALLED OR INSTALLED INCORRECTLY

• NOT WAITING FOR DEVICEREADY

• PASSING INCORRECT/MALFORMED PARAMETERS

• PAY ATTENTION TO ERROR CALLBACK!

• DOCUMENTATION IS OFTEN AN ISSUE

• CAN LEAD TO FEELINGS OF POOR QUALITY

Page 18: Common PhoneGap Gotchas (#PGDay EU 2016)

DATA STORAGE

• LOCALSTORAGE IS TEMPTING, BUT A POOR CHOICE

• SYNC IS A BIG QUESTION (COUCHDB & POUCHDB)

• USE THE BEST STORAGE OPTION FOR THE DATA

Page 19: Common PhoneGap Gotchas (#PGDay EU 2016)

localStorage WHAT IS IT GOOD FOR?NOT MUCH

Page 20: Common PhoneGap Gotchas (#PGDay EU 2016)

localStorage File API Web SQL IndexedDB SQLite (1, 2, 3) App Prefs Keychain

Quota ~5mb n/a 5 - 50mb 5 - 50mb n/a platform specific

platform specific

Async? ✕ ✓ ✕ ~ ~ ✓ ✓

Format Key / String Text / Binary Structured data Store / Key / Object Structured data Key / String Key / String

Good forNot much Caching

Temporary data

Saving and loading

documents

Storing & querying

structured data

Storing semi-structured JavaScript

objects

Large amounts of structured

dataUser Settings Sensitive

Information

Note Not persistent, No callback

Persistent and temporary file

systemsDeprecated Bugged on iOS

(buggyfill)Keep result set

small!Not good for lots of data

Not good for lots of data

Page 21: Common PhoneGap Gotchas (#PGDay EU 2016)

DON'T STORE SENSITIVE INFORMATION

Page 22: Common PhoneGap Gotchas (#PGDay EU 2016)

YOUR USER'S DATA AND PRIVACY ARE PRICELESS

Page 23: Common PhoneGap Gotchas (#PGDay EU 2016)

ALWAYS USE HTTPS

Page 24: Common PhoneGap Gotchas (#PGDay EU 2016)
Page 25: Common PhoneGap Gotchas (#PGDay EU 2016)

DON'T TRUST YOUR DATA

• VALIDATE EVERYWHERE

• DOM INJECTION

• SQL INJECTION

Page 26: Common PhoneGap Gotchas (#PGDay EU 2016)

// DOM INJECTION: let untrustedData = `<button onclick="doSomethingNefarious();"/>`; el.innerHTML = untrustedData; // SUSCEPTIBLE el.textContent = untrustedData; // SAFE

// SQL INJECTION: { let sql = "select * from user where username = " + username; db.transaction(tx => tx.executeSql(sql)); } // SUSCEPTIBLE

{ let sql = `select * from users where username = ?`; db.transaction(tx => tx.executeSql(sql, [username])); } // SAFE

Page 27: Common PhoneGap Gotchas (#PGDay EU 2016)

NETWORK CONNECTIVITY

• LOTS OF MISUNDERSTANDING ABOUT WHITELIST AND CONTENT-SECURITY-POLICY

Page 28: Common PhoneGap Gotchas (#PGDay EU 2016)

CONFIGURE THE WHITELIST

Page 29: Common PhoneGap Gotchas (#PGDay EU 2016)

Network Access

<access origin="https://www.example.com" /> <access origin="https://*.example.com" /> <access origin="*" /> <!-- NOT SECURE -->

Top-level Navigation (location .href)

<allow-navigation href="https://*.example.com/*" />

$ cordova plugin add cordova-plugin-whitelist --save

Page 30: Common PhoneGap Gotchas (#PGDay EU 2016)

CSP META TAG

• TIGHTEN AS MUCH AS POSSIBLE

• DEV WILL BE MORE PERMISSIVE

Page 31: Common PhoneGap Gotchas (#PGDay EU 2016)

<!-- Development CSP --> <meta http-equiv="Content-Security-Policy" content="default-src 'self' gap: https://ssl.gstatic.com; connect-src 'self' ws: https://test.example.com; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-eval' 'unsafe-inline' gap:;"/>

<!-- Production CSP --> <meta http-equiv="Content-Security-Policy" content="default-src 'self' gap: https://ssl.gstatic.com; connect-src 'self' https://www.example.com; img-src 'self' data:; style-src 'self'; script-src 'self' gap:;"/>

Page 32: Common PhoneGap Gotchas (#PGDay EU 2016)

'self' Required to load local resources

gap: UIWebView only (iOS) — used by web-native bridge

ws: Web socket support (needed for live reload)

https://ssl.gstatic.com Android TalkBack (Accessibility)

data: Inline images, including SVG use

'unsafe-eval' Try to avoid, but not always possible (live reload / plugins)

'unsafe-inline' Try to avoid, but not always possible (live reload / plugins)

Page 33: Common PhoneGap Gotchas (#PGDay EU 2016)

CROSS-ORIGIN RESOURCE SHARING (CORS)

• SERVER-SIDE

• REQUIRED FOR WKWEBVIEW, PHONEGAP SERVE, AND BROWSER

Page 34: Common PhoneGap Gotchas (#PGDay EU 2016)

CONNECTIVITY IS NOT GUARANTEED

Page 35: Common PhoneGap Gotchas (#PGDay EU 2016)

if (!navigator.onLine) { throw new OfflineError(); // offline; don't bother } let xhr = new XMLHttpRequest(); xhr.open(method, URI, true); xhr.timeout = 30000; // 30 second timeout xhr.ontimeout = function() { throw new TimeoutError(); // might be offline or unreachable. If online, try // again (throttle); error after too many failures // Watch for long-running queries! } xhr.onerror = function() { throw new XHRError(); // try again; error after too many failures } xhr.onload = function() { if (this.status >= 200 && this.status <= 299) { /* success! */ } else { throw new HTTPError(this.status); } } xhr.send(data);

Page 36: Common PhoneGap Gotchas (#PGDay EU 2016)

ICONS & LAUNCH SCREENS

• PATHS ARE RELATIVE TO CONFIG.XML

• USE MODERN <SPLASH> AND <ICON> TAGS

• MAKE SURE THE IMAGE IS SAME SIZE AS SPECIFIED IN CONFIG.XML

• KEEP LAUNCH SCREEN SIMPLE

Page 37: Common PhoneGap Gotchas (#PGDay EU 2016)

PERCEIVED AS SLOW

• 300MS DELAY **SIGH**

• JANKY ANIMATIONS

• PROFILE!

Page 38: Common PhoneGap Gotchas (#PGDay EU 2016)

BAN SLOW INTERACTIONS

• FAST CLICK OR GESTURE LIBRARY (1, 2)

• AFFORDANCES

• DELAY WORK UNTIL AFTER ANIMATIONS

Page 39: Common PhoneGap Gotchas (#PGDay EU 2016)

PROFILE • TRACK MEMORY, FRAMES, EXECUTION TIME

• CHROME / SAFARI + INSTRUMENTS

• SCREEN RECORDING

Page 40: Common PhoneGap Gotchas (#PGDay EU 2016)

PERFORMANCE MATTERS Quantifiable Performance

Bruce Lefebvre 11:20am

Page 41: Common PhoneGap Gotchas (#PGDay EU 2016)

DESIGN RESPONSIVELY

• HTML IS ALREADY RESPONSIVE

• RESPONSIVE UNITS (%, REM, VIEWPORT UNITS:VH,VW,VMAX,VMIN)

• MEDIA QUERIES & DEVICE PLUGIN

• FLEXBOX LAYOUT

Page 42: Common PhoneGap Gotchas (#PGDay EU 2016)

iPhone iPad (landscape)

Page 43: Common PhoneGap Gotchas (#PGDay EU 2016)

MOBILE IS QUIRKY

• MOBILE WEB IS QUIRKY

• ACCEPT DEVICE & PLATFORM-SPECIFIC WORKAROUNDS

• DEVICE PLUGIN

• CROSSWALK

• WKWEBVIEW

Page 44: Common PhoneGap Gotchas (#PGDay EU 2016)

My finger is actually here

Touch is registered here

iOS UIWebView Scroll Offset Bug

Page 45: Common PhoneGap Gotchas (#PGDay EU 2016)

# iOS 9 (UIWebView on iOS 8) $ cordova plugin add cordova-plugin-wkwebview-engine --save

# iOS 8 & 9 $ cordova plugin add https://github.com/apache/cordova-plugins.git#master:wkwebview-engine-localhost —save # Then, edit config.xml <platform name="ios"> <content src="http://localhost:49000"/> </platform>

# Android $ cordova plugin add cordova-plugin-crosswalk-webview --save

Page 46: Common PhoneGap Gotchas (#PGDay EU 2016)

MANAGE THE KEYBOARD

Handling the keyboard in hybrid applicationsTim Lancina 10:15am

Page 47: Common PhoneGap Gotchas (#PGDay EU 2016)

THANK YOU! @photokandy www.photokandy.com