cordova - guide - app development -...

19
Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview and introduction to Apache Cordova application development and design. Contents intro Cordova CLI - build initial project Cordova App - structure recap - app directory www directory index.html add Cordova specifics add some jQuery add some jQuery Mobile jQuery Mobile - test transitions jQuery Mobile - navigation intro example navigation jQuery Mobile - using widgets listviews listviews - example listviews - adding some formatted content listviews - updated example Cordova app - current design Cordova CLI - build initial project Cordova app development begins with creation of a new app using the Cordova CLI tool. An example pattern and usage is as follows for initial project creation. cd /Users/ancientlives/Development/cordova cordova create basic com.example.basic Basic cd basic creates new project ready for development cordova platform add android --save cordova build adds support for native SDK, Android then builds the project ready for testing and use on native device cordova emulate android outputs current project app for testing on Android emulator Cordova App - structure recap - app directory A newly created project will include the following type of structure for design and development.

Upload: others

Post on 09-Aug-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

Cordova-Guide-AppDevelopment-BasicsDrNickHayward

AbriefoverviewandintroductiontoApacheCordovaapplicationdevelopmentanddesign.

Contents

introCordovaCLI-buildinitialprojectCordovaApp-structurerecap-appdirectory

www directoryindex.html

addCordovaspecificsaddsomejQueryaddsomejQueryMobile

jQueryMobile-testtransitionsjQueryMobile-navigation

introexamplenavigation

jQueryMobile-usingwidgetslistviewslistviews-examplelistviews-addingsomeformattedcontentlistviews-updatedexample

Cordovaapp-currentdesign

CordovaCLI-buildinitialproject

CordovaappdevelopmentbeginswithcreationofanewappusingtheCordovaCLItool.

Anexamplepatternandusageisasfollowsforinitialprojectcreation.

cd/Users/ancientlives/Development/cordovacordovacreatebasiccom.example.basicBasiccdbasic

createsnewprojectreadyfordevelopment

cordovaplatformaddandroid--savecordovabuild

addssupportfornativeSDK,Androidthenbuildstheprojectreadyfortestinganduseonnativedevice

cordovaemulateandroid

outputscurrentprojectappfortestingonAndroidemulator

CordovaApp-structurerecap-appdirectory

Anewlycreatedprojectwillincludethefollowingtypeofstructurefordesignanddevelopment.

Page 2: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

e.g.

|-config.xml|-hooks|-README.md|-platforms|-android|-platforms.json|-plugins||-android.json||-cordova-plugin-whitelist||-fetch.json|-res||-icon||-screen|-www||-css||-img||-index.html||-js

initially,ourmainfocuswillbethe www directory

www directory

Then,anexample www directorywillbeasfollows,

e.g.

|-www||-css||-index.css||-img||-logo.png||-index.html||-js||-index.js

index.html

Theinitial index.html filewillbeasfollows.

<html><head><metahttp-equiv="Content-Security-Policy"content="default-src'self'data:gap:https://ssl.gstatic.com'unsafe-eval';style-src'self''unsafe-inline';media-src*"><metaname="format-detection"content="telephone=no"><metaname="msapplication-tap-highlight"content="no"><metaname="viewport"content="user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1,width=device-width"><linkrel="stylesheet"type="text/css"href="css/index.css"><title>HelloWorld</title></head><body><divclass="app"><h1>ApacheCordova</h1><divid="deviceready"class="blink">

Page 3: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

<pclass="eventlistening">ConnectingtoDevice</p><pclass="eventreceived">DeviceisReady</p></div></div><scripttype="text/javascript"src="cordova.js"></script><scripttype="text/javascript"src="js/index.js"></script></body></html>

Thefirstthingwe'lldoisclearoutthebodyofourHTMLdocument,andthenstartaddingsomeofourowncontentandstructure.The cordova.js scriptreferenceisalwaysaddedtoanapp,butitisnotsomethingweneedtoexplicitlyaddtotheappinstallitself.Itisprovided,bydefault,aspartoftheCordovacreateandbuildforacurrentproject.

Anexampleupdatedfilemightbeasfollows,

<body><div><h3>TripNotes</h3><p>welcometotripnotes...collectandsavetraveldata</p></div><scripttype="text/javascript"src="cordova.js"></script><scripttype="text/javascript"src="js/index.js"></script></body>

Alackofstylingwillbeanissue.

Image-CordovaApp-Basicv0.01

Page 4: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

addCordovaspecifics

Bydefault,theCordovacontainerfortheapplicationwillexposenativeAPIstoourwebapplicationrunningintheWebView.However,ingeneraltheseAPIswillnotbeavailableuntilanapplicablepluginhasbeenaddedtotheproject.ThecontaineralsoneedstoperformsomepreparationbeforetheAPIscanbeused.

Tohelpusasdevelopers,Cordovainformsuswhenthecontainer,andassociatedAPIs,arereadyforuse.Itfiresaspecificevent,calledthe deviceready event.Ineffect,anylogicwithinourapplicationthatrequiresuseofCordova

Page 5: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

APIsshouldbeexecutedafterthis deviceready notificationhasbeenreceived.

addsomejQuery

addtofootof <body>

<scripttype="text/javascript"src="js/jquery.min.js"></script>

addtestto trip.js file

functiontripNotes(){alert("JSWorking...");}

$(document).ready(tripNotes);

WecanalsoaddsomejQuery,oranotherrequiredJSlibraryorplainJS,andthenusetheapp'scontainerwithstandardJSfunctions.Forexample,outputtingasimplealert.

Image-CordovaApp-Basicv0.02

Page 6: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

addsomejQueryMobile

update head withlocaljQueryMobileCSS

<head>...<linkrel="stylesheet"type="text/css"href="css/jquery.mobile.min.css"/></head>

Page 7: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

update body forbasicapp

<body><divdata-role="page"><divdata-role="header"><h3>tripnotes</h3></div><!--/header--><divrole="main"class="ui-content"><p>recordnotesfromvariouscitiesandplacedvisited..</p></div><!--/content--><divdata-role="footer"><h5>footer...</h5></div><!--/footer--></div><!--/page--><scripttype="text/javascript"src="cordova.js"></script><scripttype="text/javascript"src="js/index.js"></script><scripttype="text/javascript"src="js/jquery.min.js"></script><scripttype="text/javascript"src="js/jquery.mobile.min.js"></script><scripttype="text/javascript"src="js/trip.js"></script></body>

Image-CordovaApp-Basicv0.03

Page 8: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

jQueryMobile-testtransitions

WecanalsoaddsomejQueryMobiletransitions,andtestsomebasicinternalnavigation.

update index.html toaddpagecontainers,transitions...

<!--page1--><divdata-role="page"id="page1">

Page 9: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

<divdata-role="header"><h3>tripnotes</h3><p>recordnotesfromvariouscitiesandplacedvisited..</p></div><!--/header--><divrole="main"class="ui-content"><p>View-<ahref="#page2"data-transition="slidedown">page2</a></p></div><!--/content--><divdata-role="footer"><h5>footer-page1</h5></div><!--/footer--></div><!--/page1--><!--page2--><divdata-role="page"data-dialog="true"id="page2"><divdata-role="header"><h3>page2</h3></div><!--/header--><divrole="main"class="ui-content"><p><ahref="#page1"data-rel="back">Cancel</a></p></div><!--/content--><divdata-role="footer"><h5>footer-page2</h5></div><!--/footer--></div><!--/page2-->

Image-CordovaApp-Basicv0.04

Page 10: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

Image-CordovaApp-Basicv0.05

Page 11: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

jQueryMobile-navigation

WemayalsousejQueryMobiletomanagethenavigationstackwithintheCordovaapp.

intro

So,we'vejustaddedsomejQueryMobiletoaninitialCordovaapp.Thisincludedsomeinitialpages,transitions,andbasicnavigation.

Page 12: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

TohelpusbuildthisoutwithjQueryMobile,forexample,we'llbrieflyneedtolookatnavigationwithinourapps.Forthepurposesofmobiledevelopment,thisnavigationthankfullyfollowsanasynchronouspattern.

So,navigationinjQuerymobile,forexample,isbaseduponloadingpagesintotheDOMusingAJAX.Thiswillmodifythepage'scontent,andthenre-renderfordisplaytotheuser.Itwillalsoincludeasetofaestheticallypleasing,anduseful,animationstohelpinformtheuserofchangesinstate,andthereforeappropriateupdatesinthecontent.

Thisnavigationsystemeffectivelyhijacksalinkwithinapage'scontentcontainer,andthenroutesitthroughanAJAXrequest.Thebenefitfordevelopersisaparticularlyuseful,andalmostpainless,approachtoasynchronousnavigation.Mostofthetime,wearenotevenawareofthisupdatedrequest.Inspiteofhijackingthelinkrequest,itisstillabletosupportstandardconceptssuchasanchorsanduseofthebackbuttonwithoutbreakingthecoherenceandlogicoftheapplication.

Therefore,jQueryMobileisabletoloadandviewgroupsofdisparatecontentinpageswithinourinitialhomedocument.Inessence,themanycombiningtoformtheonecoherentapplication.

ItssupportforcoreJavaScripteventhandling,inparticularforURLfragmentidentifierswith hashchange andpopstate ,allowstheapplicationtopersist,atleasttemporarily,arecordofusernavigationandpathsthroughthe

content.Wecanalsotapintothisinternalhistoryoftheapplication,andagainhijackcertainpatternstohelpusbetterinformtheuseraboutstatechanges,differentpaths,content,andsoon.

examplenavigation

ThefollowingisanexampleofusingthejQueryMobilestandardnavigatemethod, $.mobile.navigate ,whichisusedasaconvenientwaytotrackhistoryandnavigationevents.

Withthissimpleexample,wecansetourrecordinformationforthelink,effectivelyanyusefulinformationforthelinkoraffectedchangeinstate.Wecanthenlogtheavailabledirectionfornavigation,inthisexamplethefactwecangoback,theurlforthenavstate,andanyavailablehash.Inourexample,thesimpleappendedhashintheurl, #nav1 .

Whatthisallowsustodoiskeepaclearrecordofusertraversalthroughtheapplication,logitasrequiredbygivenstatechanges,andthenuseittoinformourapplication'slogic,ouruser,andsoon.

Demo-jQueryMobilenav

jQueryMobile-usingwidgets

Withinourapp'swebviewcontainer,wecanaddstandardHTMLelementsforanyrequiredcontentcontainers.

Forexample,standardHTMLandHTML5elementssuchas p , headings , lists , sections ,andsoon.

Thankfully,wedon'tnecessarilyhavetobuildthewholeapplicationfromscratch.

jQueryMobileincludesawide-rangeofpre-fabricatedwidgetswecanaddtoourapplications.Thesearenaturallytouch-friendly,andincludecollapsibleelements,forms,responsivetables,dialogs,andmanymore.We'vealreadyseenanexamplewiththeoverall pageContainer widget.

listviews

Agoodexampleofthistypeofpre-fabricatedwidgetisalistview.

jQueryMobilehelpsusstyle,renderandthenmanipulatestandarddataoutputandcollections,includingrenderingoflistsasinteractive,animatedviews.

Theselistsarecodedwitha data-role attribute,aswesawearlierwithapagevalueforadatarole...

data-role="listview"

Thisallowsustostyleandrenderourlistswithadditionaloptionssuchasadynamicsearchfilter.

Page 13: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

Demo-jQueryMobilelistview1Demo-jQueryMobilelistview2

listviews-example

<!--listviewexample--><div><uldata-role="listview"><li>Cannes</li><li>Marseille</li><li><ahref="#page3"data-transition="slide">Monaco</a></li><li>Nice</li></ul></div>

simplelistviewwithslidetransition

<!--page3--><divdata-role="page"id="page3"><divdata-role="header"><h3>page3</h3></div><!--/header--><divrole="main"class="ui-content"><p><adata-rel="back"class="ui-btn">Return</a></p><sectionclass="image-view"><imgsrc="media/images/monaco1.jpg"><section></div><!--/content--><divdata-role="footer"><h5>footer-page3</h5></div><!--/footer--></div><!--/page3-->

newpageforMonacoimageDemo-jQueryMobilelistview3

Listviewsarealotoffunandveryusefulforeasilyorganisingourdata,aswe'vejustseen.

However,wecanalsousealistviewtoaddfilteringandlivesearchoptionstoourlists.

Wesetasimpleclient-sidefilterbyaddinganattributefor data-filter ,andthensetthevalueto true

data-filter="true"

jQueryMobilewillthenaddasearchqueryinputfieldtothetopofourlistwidget,andsetafilterfortheenteredsearchquery.Effectively,it'sperformingapatternmatchusingapartiallyenteredstringagainststringsinadesignatedlist.Itcanmatchpartialfragmentsofalistitem,andthendynamicallyfilterourlistcontent.

Wecanalsosetsomedefault,helpfultextfortheinputfield.Ourwayofpromptingtheusertointeractwith,andthereforeusethisfeaturecorrectly.

data-filter-placeholder="SearchCities"

Totidyupthepresentationofourlist,wecanalsoaddaninsetusingtheattribute

data-inset="true"

Now,wehaveamuchbetterdesignforourlists,andsomeusefulfilteringoptionsaswell.

Demo-jQueryMobilelistview4

Page 14: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

listviews-addingsomeformattedcontent

OneofthefunaspectsofworkingwithaframeworksuchasjQueryMobile,andotherssuchastheexcellentIonicframeworkandOnsenUI,isthesimplewaywecanorganiseandformatourdatapresentationsandviews.

Forexample,ifwehaveagroupeddataset,wecanstillpresentitusinglists.However,wecanalsoaddinformativeheadings,linkstodifferentcategorieswithinthisdataset,andsimplestylingtohelpdifferentiatecomponentswithinthelistinterface.

Therefore,westructurethelistasnormal,withsub-headings,paragraphs,andsoon.Then,jQueryMobilegivesusasimpleoptionforsettingcertainlistcontentasanaside.Forexample,

<pclass="ui-li-aside">1image</p>

Therearemanysimilartweaksandadditionswecanaddtohelpimproveorganisationandrenderingoflistdata.Furtherdetailscan,ofcourse,befoundinthejQueryMobileAPI.

listviews-updatedexample

<uldata-role="listview"data-inset="true"><lidata-role="list-divider"role="heading">FrenchCities</li><li><ahref="#page3"data-transition="slide"><h3>Monaco</h3><p><strong>PrincipalityofMonaco</strong></p><p>Monacoisasovereigncity-state,andformspartoftheFrenchRiviera...</p><pclass="ui-li-aside"><strong>1</strong>image</p></a></li><li><ahref="#"><h3>Nice</h3><p>LocatedinthesouthofFrance,closetotheborderwithItaly...</p></a></li></ul>

Demo-jQueryMobilelistview5

Image-jQueryMobile-addsomeorganisation

Page 15: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview
Page 16: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

Image-Cordovaapp-TripNotes-example1

Page 17: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

Image-Cordovaapp-TripNotes-example2

Page 18: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview

Cordovaapp-currentdesignOurcurrentdesignincludesasimpleheader,main,andafooter.Tohelpwiththerenderingofourapp'spage,wecanfixthisfootertothebottomoftheviewbyadding,forexample,thefollowingattributeintheHTML,

<divdata-role="footer"data-position="fixed"><h5>footer-page3</h5></div><!--/footer-->

Wecansetthisattributeonanyofourfootersectionsinanyoftheviews.

Image-Cordovaapp-TripNotes-example3

Page 19: Cordova - Guide - App Development - Basicscsteach422.github.io/assets/docs/extras/cordova-app-basic-dev.pdf · Cordova - Guide - App Development - Basics Dr Nick Hayward A brief overview