navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

Up and Running with PhoneGap

Up and Running with PhoneGap

with Ryan Stewart

 


Find out how to build and package native device applications using Adobe PhoneGap and web technologies such as HTML, CSS, and JavaScript. This course shows how to access the sensors and other capabilities of mobile devices through JavaScript coding, and build applications that reuse most of their code for deployment to iOS, Android, BlackBerry 6, and other mobile operating systems. Author Ryan Stewart shows how to get started with PhoneGap, including installing and setting up the software development kit (SDK), building a simple application from templates, using the most popular APIs like Geolocation and Storage, integrating jQuery Mobile and other JavaScript frameworks, and testing and debugging your app.
Topics include:
  • What is PhoneGap?
  • Installing the device simulators and native SDKs
  • Starting a web project with PhoneGap templates
  • Adding an interface with jQuery
  • Using the Connectivity, Camera, and other APIs
  • Compiling a PhoneGap application
  • Extending PhoneGap
  • Testing and debugging with PhoneGap Build

show more

author
Ryan Stewart
subject
Developer, Web, Mobile Web
software
HTML , JavaScript , PhoneGap
level
Beginner
duration
1h 57m
released
Feb 20, 2013

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:00(music playing)
00:04Hi! I'm Ryan Stewart, and I'm going to get you up and running with PhoneGap.
00:08This course will show how web developers can build mobile applications with
00:12languages and tools they know.
00:14I'll cover why you'd want to use PhoneGap and how to get started setting up an application.
00:19In this course I'll start by showing you how to begin with a basic HTML page and
00:23turn that into a mobile application with PhoneGap.
00:26See how to use command line tools that come with PhoneGap to generate a project
00:30you can use as a base.
00:32You'll see how to use some of the core APIs so you can leverage device features
00:36without ever touching native code.
00:38You'll see tips for how to debug, test, and emulate your application before you deploy it.
00:44Finally, you'll see how you can extend PhoneGap and how you can target multiple
00:48platforms with PhoneGap Build.
00:50After going through this course, you will have seen how PhoneGap works, so you
00:53can start building mobile applications with HTML, JavaScript, and CSS.
Collapse this transcript
What you should know
00:00You don't have to know a lot of programming to be successful in this course, but
00:04you should be familiar with web development.
00:06Since PhoneGap exposes device features to applications built in HTML, JavaScript,
00:11and CSS, knowledge of those languages is important.
00:15Knowledge of jQuery isn't required but will help in understanding some of the samples.
00:20This course uses the Terminal during a few movies, so being aware of how to use
00:24Terminal and command line tools will also make the course easier to follow.
00:29Finally, while you don't need to know native mobile development, knowing how to
00:33use native IDs like Xcode and Eclipse will help.
00:36If you aren't familiar with some of the technologies we will be using, I'd
00:39recommend checking out these courses here in lynda.com online training library.
Collapse this transcript
Using the exercise files
00:00If you're watching this tutorial, you'll have access to the exercise files.
00:04The exercise files are organized by chapter and movie, and I've put them on the
00:08desktop to make them easy to access.
00:11Most of the course builds on previous chapters, so the exercise files follow
00:15along and add functionality as we go.
00:18Included in the exercise files are code snippets.
00:21These snippet files are blocks of code that I'll copy and paste into the
00:25application as I build it.
00:27In addition to the exercise files, you'll want to make sure you have a text
00:30editor handy that you can use to edit the HTML, JavaScript, and CSS.
Collapse this transcript
1. Overview of a Mobile Web Application
What is a mobile web application?
00:00There are many different ways to deploy content to devices for developers.
00:04The main distinction is between content that runs inside the device's web
00:07browser, just like any website, and the content that runs as an application on the device.
00:14Mobile applications give you a lot of benefits.
00:16They give you access to device features and sensors that aren't normally
00:20available to websites.
00:21These are things like camera, offline storage, and information on the device like
00:26what's in the phone's contact list.
00:29All the major platforms tend to have very different ways to build
00:32mobile applications.
00:34They each require their own tools and languages to deploy applications and
00:38create content for them.
00:39A good example is Xcode and Objective-C for building iOS applications, and
00:45Eclipse and Java for building Android applications.
00:48Some technologies allow you to build hybrid applications, or mobile applications
00:53that use web technologies.
00:55These hybrid applications let you build mobile applications using HTML,
01:00JavaScript, and CSS that's supported by the device.
01:04They also have access to the exact same device APIs and sensors and
01:08information as regular mobile applications, but the user interface is mainly
01:13HTML, JavaScript, and CSS.
01:16These applications can also be deployed and distributed to app stores in the
01:20exact same way as other mobile applications. That means to the user,
01:24there won't be a difference between a hybrid application and the native
01:27mobile application.
01:29Mobile web applications provide the functionality of native apps but use web technologies.
01:34This makes it easy for web developers to take advantage of the application
01:37ecosystem and build apps using languages and technologies that you know.
Collapse this transcript
The responsive layout (handhelds / tablets)
00:00One of the critical parts of creating applications for devices is taking into
00:04account the form factor.
00:05This includes the size of the screen or the resolution of the device.
00:10There are many applications that are built specifically for phones or
00:13specifically for tablets, but there are some inherent web technologies that can
00:17be used to target both with mobile web applications.
00:21One of the most common tactics for creating content that works across devices is
00:25with responsive design.
00:27Responsive design lets you specify different CSS files for different form factors.
00:32So instead of creating entirely new content or an entirely new application for
00:36smartphones or tablets, you can reuse content and simply swap out new CSS rules
00:42depending on the size of the screen.
00:43The easiest way to do this is with CSS media queries, which lets you specify a
00:49different set of CSS rules and CSS content for certain parameters like screen
00:53size or even device dpi.
00:56Within mobile web applications, you can use media queries in an application
01:00context, so that your application will use different style sheets depending on
01:04the form factor of the device.
01:07Some of the examples of when you would want to use CSS media queries within
01:10PhoneGap would be if you want to change the design for portrait versus
01:14landscape, if you want to change the design for a form factor, if you want to
01:18change the design for a tablet, and if you want to alter the design for retina
01:22and other high-dpi screens.
01:25Responsive design is beyond the scope of this course, but for more information
01:28about responsive design fundamentals and creating responsive web design,
01:32check out these courses here in the lynda.com online training library.
01:35By thinking about responsive design when building your mobile web application,
01:38you can create something that will work across phones and tablets while
01:42leveraging the same content.
Collapse this transcript
Creating an HTML application template to use in the exercises
00:00To create a mobile web application, the first step is to build out an HTML page,
00:04just like you would if you were building a mobile website.
00:07However, there are a few HTML meta tags which will make your application behave
00:11like an application and not like a website.
00:14I am going to switc over to Sublime Text, although you can use any text editor
00:17that you want, and we're going to start off by just building a basic HTML page.
00:22So I will set the DOCTYPE to html.
00:25We'll set the html tag, start with our head.
00:31I'll set my first meta tag, which will set the character set to UTF-8, so the
00:38browser knows how to render it.
00:40Normally, when we're browsing websites on a mobile phone and the browser
00:44encounters a phone number, it will often make that phone number link
00:47clickable, so that we can click it from our browser and it will take us out to the call screen.
00:52This is handy within the context of a mobile website, but in an application when
00:56we want to control the entire experience, it's not as desirable.
01:00Luckily, we can use metadata tags to specifically instruct the browser not to
01:06create links from those phone numbers.
01:08To do that we're going to create a meta tag and we're going to give the name
01:13attribute a value of format-detection. The content attribute is going to be
01:18set to telephone-no.
01:21That's going to make sure that this browser doesn't turn any phone numbers
01:25into clickable links.
01:28The next phone we're going to create is all about how the viewport or how the
01:31screen renders to the user.
01:33By default, when you're browsing a website on a phone, you have a lot of control
01:37over pinching and zooming and setting the scale of how that content is
01:41rendering. But in an application you don't have that same control,
01:45so we need to use metadata to make sure that our HTML content is going to
01:50behave more like an application, when it comes to how the user can interact with the screen.
01:55So to modify that, we'll set another meta tag, and the name attribute for this
02:00is going to be viewport, and the content attribute is going to be a string of items.
02:09The first one we're going to set is user-scalable, and we want that to be no.
02:14This just means that the user won't be able to pinch and zoom our content;
02:18it's going to be static in one place and always the same size.
02:22We also want to set the initial- scale to 1, and we're going to set the
02:26minimum-scale to 1, as well as the maximum-scale to 1. And this is just so that
02:34the user will never see a zoomed-out or zoomed-in version of the application.
02:38It's always going to be displayed at a 1:1 ratio.
02:42Next, we want to set the size of the viewport, and we want to make sure that the
02:45viewport or our HTML content is taking up the entire screen.
02:49So we can make sure that it's doing that by saying width is equal to
02:53device-width and height is equal to device-height.
02:59Lastly, we want to make sure that our content is being rendered at the same dpi
03:03that's on the device.
03:04DPI is a measure of how many pixels are on the screen per inch, and so some
03:08high-resolution displays will have more pixels than some of the lower-resolution
03:11displays. And we want to make sure that our content is being rendered at the
03:15native dpi for that device.
03:17We can do that by setting target- densitydpi equal to the device-dpi.
03:25So those two meta tags will give us the exact experience that we want, from an
03:29application standpoint, and make sure that our content doesn't behave the way it
03:33would if it were being rendered inside of a regular mobile web browser.
03:38The next thing we're going to do is set a title for our application, and this
03:41title isn't going to be visible anywhere to the user, so we can say whatever we want.
03:45Just as a placeholder we'll use Our Application.
03:49We'll specify the actual title of the application later on in the PhoneGap
03:52configuration files.
03:55We also want to create a link to an external style sheet, which is going to be
03:59where we will style and customize the look and feel of our application. So we'll do
04:03link rel="stylesheet" and we'll set href="css/main.css."
04:16That's all we need for our head tag, so we'll go ahead and close the head, and
04:21we'll create a body tag with a placeholder div tag in it for our content later
04:28on, and we'll close the body and finally close the HTML tag.
04:33To make sure your mobile web applications behave like applications and not like
04:38websites, use the meta tag with specific attributes to control behavior.
04:43Even though all of this is running as HTML, JavaScript, and CSS, these meta
04:47tags are important for an application experience, as opposed to a mobile
04:51website experience.
Collapse this transcript
2. Overview of PhoneGap
What is PhoneGap?
00:00PhoneGap lets you build mobile applications with HTML, JavaScript, and CSS.
00:05It exposes native APIs and functionality through JavaScript so you can use
00:10web technologies to create applications with full access to device sensors and features.
00:15This means that as a web developer
00:17you have full access to what the device can do.
00:20PhoneGap uses something called a web view, and provides a full-screen chromeless
00:24browser window that uses the underlying HTML renderer on the device to display content.
00:30This means that if your application is running in, say, iOS, your content will be
00:34displayed using the mobile WebKit renderer.
00:38If your application is running on Windows Phone, it will be using the same HTML
00:42renderer that Internet Explorer uses.
00:45PhoneGap comes with a JavaScript library that acts as a native bridge
00:49between JavaScript's code and the native API, which gives you access to all
00:53the device features.
00:55This means that as a developer, you don't have to write native code;
00:58you simply write against that JavaScript API to access the device features.
01:03PhoneGap applications are packaged and deployed just like any other mobile
01:07application. They can be put on app stores and marketplaces and distributed in the same way.
01:13Users will also download them and use them in the same way that they would
01:16any other application.
01:18One of the benefits of PhoneGap is instead of having to write a different
01:21application for every platform with a whole new set of tools and technologies,
01:25you can take a single chunk of HTML, JavaScript, and CSS content and build an
01:30application for multiple platforms.
01:32PhoneGap currently supports iOS, Android, Windows Phone, Blackberry,
01:37Symbian, WebOS, and Bada.
01:40PhoneGap provides web developers a way to create mobile applications that have
01:44access to device APIs.
01:46The entire UI is HTML, JavaScript, and CSS, meaning that many of the same
01:51workflows for web design and development translate perfectly to building
01:55PhoneGap applications.
Collapse this transcript
PhoneGap apps are mobile web applications
00:00One of the key differentiators of PhoneGap is the fact that as a developer
00:04you're writing HTML, JavaScript, and CSS, and that's exactly what's going to
00:08display to the user.
00:10Because PhoneGap runs within a browser instance, the HTML content you create
00:14will render the same way as it would ifitwe were running in a browser on the
00:18device. But with PhoneGap you have access to all the device's sensors and APIs
00:23that native applications do.
00:25The web view that PhoneGap runs in acts as a full-screen browser instance, and it
00:30renders your content the same way that it would if it were being displayed in
00:33the browser on the device. Because PhoneGap is all HTML, JavaScript, and CSS, that
00:39means you can use any framework or technology with PhoneGap that you would use
00:43with other HTML-based projects.
00:45So, if you're familiar with jQuery or you want to use LESS and Sass or
00:49Backbone and JS or others, you can incorporate those and use those within your PhoneGap project.
00:54PhoneGap still has access to almost every device-specific feature.
00:58PhoneGap exposes the sensors and device APIs through JavaScript, so that means as
01:03a web developer, you're not writing native code;
01:05you're simply writing JavaScript calls to access those device features like
01:09camera, geolocation, and storage.
01:12PhoneGap applications are also distributed the same way as other applications.
01:16That mean users interact, download, and purchase PhoneGap applications in the
01:21same way as they would any other mobile application.
01:24With PhoneGap you can use web technologies you use on other projects within
01:27your mobile application.
01:29The PhoneGap JavaScript API gives you access to device features like camera,
01:33notifications, device-specific information, and offline storage.
01:37Even though the development and user interface is in HTML, JavaScript, and CSS,
01:42PhoneGap applications work the same way as other mobile applications.
Collapse this transcript
Extending the mobile web template with PhoneGap
00:00The JavaScript library that PhoneGap provides is the link between your HTML,
00:04JavaScript, CSS code, and the native device APIs.
00:08It includes all of the functionality and events that are needed to create a
00:11mobile application with access to the sensors and events on the device.
00:16That JavaScript file is specific to each platform, and is available after
00:20you download PhoneGap.
00:21So let's go ahead and take a look at downloading PhoneGap.
00:24To download PhoneGap you can just go to phonegap.com, and the Download button
00:29should be the first thing that you see. If it's not there for any reason, you
00:33can just go to phonegap.com/download and you'll have the download link, there,
00:37the big blue button. So go ahead and download that.
00:39When it's finished downloading, go ahead and go to the location where you
00:45downloaded the file and unzip it, and now we have PhoneGap on our computer.
00:52When you go into the PhoneGap folder, you'll see there is a folder called doc.
00:55This is the documentation that ships with PhoneGap.
00:58So if you ever have need of offline PhoneGap documentation, you can just
01:01point a web browser at this doc folder and you'll access to all the PhoneGap
01:05docs for this version.
01:06The files we're looking for is in the lib directory.
01:09In the lib directory are all the folders for all the platforms that PhoneGap
01:13supports. The one we're looking for right now is iOS.
01:17And then we'll go into the CordovaLib directory, and this cordova.ios.js file is
01:22the one that we want to copy over to our project so we can use the device-
01:26specific features and the APIs from this library.
01:29So we'll copy this and place it in our exercise folder, and now I'm going to
01:35open up the index.html file and start adding PhoneGap-specific API calls to my project.
01:41The most important event inside of a PhoneGap application is the device ready event.
01:45This is an event that gets sent by the native platform to our PhoneGap
01:49application that tells PhoneGap it's ready to call the native device APIs,
01:54that the device is essentially ready to handle all of our calls.
01:58Let's start by adding an onload event to the body. So we're going to say onload,
02:03and we'll call that function init.
02:05The next thing we want to do is create our script blocks, and the first script
02:10do we need is that cordova.ios script that we just brought in.
02:14PhoneGap is actually an open-source distribution of Cordova, so that's why it's
02:16called Cordova, and we'll talk about that in a later section.
02:20So we'll say script, and the source for this script is going to be cordova.ios.js.
02:29And then we'll close that script tag, and we'll open up a new one for the scripts
02:34that we're going to code.
02:37The first thing we'll add is that init function, so we'll say function init.
02:43Within this init function we're going to create an event listener on the
02:47document to listen for that device ready event, so we'll say
02:50document.addEventListener and we're going to be listening for the
02:56deviceready event. And this is a PhoneGap-specific event, so you have to
03:00include the cordova.ios.js library for this to work. And then the callback
03:05function we're going to use is onDeviceReady, and we'll set that to false, so
03:12it doesn't propagate.
03:14Next, we need to create our onDeviceReady function, so we'll say
03:17function onDeviceReady.
03:22Everything within this function is going to be called after the device ready
03:25event has successfully fired, so that's telling PhoneGap everything is ready for
03:29the device and you can call device sensor APIs successfully.
03:33To make sure this function is fired successfully, we'll say alert('It works!')
03:38with an exclamation mark,
03:40so that then when the event fires and it calls that device ready function,
03:44we'll see an alert pop up that says it works and we'll know that we can now call device APIs.
03:49The cordova.js file and the onDeviceReady event are the two cornerstones of PhoneGap.
03:54cordova.js provides all of the APIs that you will need to access device
03:58features. Listening for the onDeviceReady event will ensure that the
04:01application can call those APIs.
Collapse this transcript
3. Setting Up Your Development Environment
Installing the device simulators and native SDKs
00:00Most of the editing that this course will need will be done in a text editor,
00:04but for compiling and debugging, the native SDKs can provide helpful features.
00:08We're going to focus first on getting iOS set up.
00:11To do iOS development, you need to download Xcode,
00:15and Xcode is available from the Mac App Store.
00:17Simply open the Mac App Store, search for Xcode, and you can download Xcode from there.
00:22I have already got it installed, so I can go ahead and open it.
00:26And there are a couple of things in Xcode that we want to make sure we have
00:30installed so that we can compile and test PhoneGap applications.
00:35Go to Xcode > Preferences > Downloads and you'll see a list of tools that you can
00:41optionally configure and install.
00:44We want to make sure that the iOS 5.1 Simulator is installed.
00:50And we also want make sure that Command Line Tools are installed.
00:53With these two things installed, we now have access to the Xcode Command Line
00:57Tools, which I'll use later to show off the PhoneGap Command Line interface.
01:01We also have access to he iOS Simulator, which we'll be using to test and debug
01:06our PhoneGap content.
01:07Using the iOS Simulator, we'll be able to debug things like location, so we can
01:12test our geolocation APIs, and we'll also be able to set specific parameters
01:16around device size and version.
01:22It's important to remember that Xcode is only available for Mac OS X, so if
01:25you want to build PhoneGap applications with Xcode, you'll have to be working on a Mac.
01:30The same is true for Windows.
01:31If you want to create content for Windows Phone using the native tools, you'll
01:35have to be on a Windows machine.
01:37The next SDK we're going to go get is the Android SDK, and the Android SDK
01:42is available for both Mac and Windows, so you can use it on whichever
01:45platform you're on.
01:46To get started with the Android SDK, go to developer.android.com/SDK/index.html.
01:55The easiest way to get the Android SDK is just to download the SDK bundle for Mac.
02:00This will include Eclipse as well as the Android plugin.
02:03It will include the SDK tools, and it will also include the Simulator that you'll
02:07need to test PhoneGap content in Android.
02:12When that's finished downloading, you can unzip the ZIP file, and inside that
02:20unbundled ZIP file you'll find all the tools that you need.
02:24The one we're going to use is Eclipse.
02:25Now you can run Eclipse from anywhere, but you may want to copy the Eclipse
02:29folder to somewhere more permanent on your hard drive than the Download folder,
02:33so you can use it later on.
02:35The first thing you'll see is the Workspace Launcher.
02:37This is where you have to tell Eclipse and the Android SDK where you want your
02:42documents and projects to go.
02:43You can use the default, which is just fine, and push OK.
02:47Next it will prompt me to contribute usage statistics, which is lets Google know
02:51how people are using the product.
02:52I'll select No, but you can select Yes; it won't affect the project at all.
02:56And now I'm in a screen where I can start creating my application.
02:59The tool that we'll be using the most is called DDMS, and if you close that
03:04initial window out and open up the other perspective, you'll see DDMS.
03:11This is going to be where we control the emulator and we're able to look at
03:14things that are happening, including console output, and it'll be the central
03:17place when we do Android development using PhoneGap.
03:21The next platform we're going to download and install is Blackberry.
03:24To download the Blackberry SDK, I can go to
03:26https://developer.blackberry.com/html5/download.
03:34There are a bunch of different SDKs that you can download for Blackberry.
03:37The one that we're most worried about today is the Blackberry 10 Beta SDK.
03:41And specifically, we're going to download the Ripple emulator as well as the WebWorks SDK.
03:47I'll first start by downloading the Ripple emulator.
03:50The Ripple emulator for Blackberry makes it possible to emulate and test
03:54content for a PhoneGap application from within Chrome and the browser itself.
03:58It's installed as a browser extension, so there are a few steps you'll have to
04:02go through to make sure that it gets set up correctly.
04:04First, go ahead and download the .crx file. And it will try to add it
04:08automatically to Chrome, but Chrome recently disabled the ability to
04:12download and install extensions automatically, so we're going to have to do a manual install.
04:16We're going to start by going to about:settings, and then we can click on the
04:24Extensions tab and it's going to tell us we have no extensions or you may
04:27have extensions already installed in Chrome. And I'm going to go find my download location,
04:33so go open the Finder. And then I am just going to drag this ripple_ui.crx file
04:38over to my Extensions folder.
04:40And it will go ahead and ask me if I can install it.
04:43I'll click Add, and now I have the Ripple Emulator (Beta) added to Chrome.
04:51You'll also want to make sure you have the SDK to package your application.
04:54So to do that, we can just download the Blackberry 10 WebWorks SDK for Mac OS X.
05:00Blackberry includes a Simulator that runs in VMWare.
05:02To get the Simulator working, you've to download the VMWare appliance and run it
05:06with the appropriate VMWare software for your platform.
05:09We're primarily going to use the Ripple emulator, so you'll not need the
05:12Simulator for this course.
05:14With the native SDKs installed, you can use the native tools for debugging and testing.
05:19In addition, PhoneGap can use the native SDKs to package up mobile applications
05:23for app stores and marketplace distribution.
Collapse this transcript
4. Creating an Application
What we'll build
00:00The application we're going to build is a note-taking application.
00:03I'm going to be using the iOS Simulator because it's a very popular choice,
00:07but you can follow along with any of the supported platforms.
00:11The first API we use is the connectivity API, to check and see if the
00:15application is offline.
00:17Clicking the New Note button brings up the Note screen.
00:20All of the notes are geotagged with the geolocation API
00:23so I can see where I am on the map.
00:24Clicking the Add a Photo button brings up the photo library, so I can save
00:29a photo with my note.
00:30We're using jQuery mobile to render the UI form elements.
00:42When I scroll down and click the Save Note button, it'll be saved when offline
00:46database using the local storage API,
00:48so it will be available when I open the application next.
00:56With this application, I introduced some of the PhoneGap APIs.
00:59At the end of the chapter, I'll show you some of the other APIs, so you'll have
01:03more information and can extend the application however you want.
Collapse this transcript
Starting a project with the iOS template
00:00PhoneGap comes with a number of tools that can help in the packaging and testing
00:04of mobile applications.
00:06Among these are the command line scripts that leverage the native SDKs and what
00:10you create, package, and debug with a single command.
00:15In this video we'll walk through the steps to create a PhoneGap application and
00:19make sure you're familiar with the PhoneGap project structure.
00:22PhoneGap is a distribution of the open-source Cordova project, and Cordova is
00:27part of the Apache foundation,
00:28so it's open source under the Apache open-source license.
00:32Right now there isn't a lot of difference between the code in Cordova and
00:35the code in PhoneGap.
00:36Cordova is divided into individual operating system projects, while PhoneGap
00:41includes all supported mobile platforms.
00:43If you haven't done so, go ahead and download PhoneGap, and you can do that by
00:48going to phonegap.com/download.
00:52Once you've downloaded it, go to the location on your hard drive where it's
00:56downloaded; for me it's in the downloads folder.
01:01Go to the lib folder, and here are folders for all the projects that PhoneGap supports.
01:05The one we're going to focus on is iOS.
01:09Here is all the code that makes that native bridge connect to JavaScript and
01:13HTML and lets you build mobile applications with HTML, JavaScript, and CSS.
01:18Included in this folder is a bin folder with a bunch of command line tools that
01:23makes it easy to create, generate, and test PhoneGap projects.
01:27Open up the Terminal and go to that directory.
01:31For me, it's Downloads/phonegap/lib/ios/bin/.
01:41Now we'll use the Create command line tool to generate a skeleton project.
01:46Now, by default the command line tools will use this templates directory
01:50to create the project.
01:52You can see when I go into it
01:54I've got the Xcode project template, as well the www folder, which has all the
01:58HTML, JavaScript, and CSS that will be created when I generate this project
02:03from the Command Line.
02:04Go back to Terminal and type /create, and you are going to have to set a
02:09couple of parameters.
02:10The first one is the location for where this project will be created, and we're
02:14going to put this on the desktop. And we'll call it ios-project.
02:20Second, you need to give it the ID, which is usually a reverse domain, something
02:24like com.example.ios. It should be unique.
02:29Lastly, we have to give it the name of the project, and we'll call this
02:33ios-project. Push Enter and that script will create that project on the desktop.
02:41Go ahead and browse to the desktop in the Finder window, double-click on
02:47ios-project, and you can see here is our Xcode project, which includes all the
02:51files that we just generated.
02:54We'll go ahead and click Xcode project to open it in Xcode, and we can now see
02:59our PhoneGap project running here in Xcode.
03:01There is the www folder, which contains our HTML, JavaScript, and CSS files, as
03:08well as all the native files which go into making a PhoneGap project.
03:13One of the benefits of using a tool like Xcode is that we can use the tools that
03:17are part of Xcode for testing and debugging our content.
03:21In this case, I can use the iOS Simulator to test how my content will look on a device.
03:26To do that, I select iPhone 6 Simulator and then push the Run button.
03:34That's going to compile my application, and it's going to install it on the Simulator.
03:38So I can now see that deviceready is fired, and I'm ready to start
03:42writing PhoneGap code.
03:43We're going to use this project going forward, so keep this on your desktop, and
03:47we'll be referring back to it.
03:48Using the command line tools in conjunction with tools from the native SDK
03:52provides everything you need to create and customize a PhoneGap application.
03:56Let's move on and get started building our application.
Collapse this transcript
Starting a project with the Android template
00:00PhoneGap comes with a number of tools that can help in the packaging and
00:03testing of mobile applications.
00:05Among these are the command line scripts that leverage the native SDKs and let
00:09you package, create, and debug with a single command.
00:13In this video we will walk through the steps to create a PhoneGap application
00:17and make sure you're familiar with the PhoneGap project structure.
00:21PhoneGap is actually a distribution of the open-source Cordova project, and
00:25Cordova is part of the Apache foundation and is open sourced under the Apache
00:29open-source license.
00:31There isn't much difference currently between the Cordova code and the PhoneGap code.
00:35The main difference is Cordova is divided into individual operating-system
00:39projects while PhoneGap includes all supported mobile platforms.
00:44If you haven't downloaded PhoneGap, go ahead and download it at
00:46phonegap.com/download, then go to the location where you downloaded it, from
00:51Downloads, double-click on it, go into the lib folder, and you'll see a folder
00:56for every platform that PhoneGap supports.
00:59The one we're concerned with is android, so double-click on that.
01:04Here you'll see the Java files and the JavaScript files that make up a
01:07PhoneGap Android project.
01:09In the bin folder are number of scripts that will help us create and test our
01:15content, and if you look in the templates folder, you'll see the skeleton of the
01:20project that gets created when you use these command line scripts.
01:23There's the Java file, the Android manifest, and then in the assets file, inside
01:28the www folder, you'll see the HTML, JavaScript, and CSS files that make up the
01:34PhoneGap project that gets created when you use these command line scripts.
01:37Go ahead and open up the Terminal.
01:39One of the first things that we have to do is set the environment variables to
01:42point to the Android SDK so the command line scripts can use the proper tools.
01:47On Windows you can set your environment variables by right-clicking on My
01:50Computer and clicking Properties. Then you should be able to change them in the
01:54Advanced System Settings menu.
01:56On Mac we want to create a profile for our Terminal, and we do that by typing, in
02:02the Terminal, touch ~/.bash_profile.
02:09That's going to create the profile that we'll use.
02:11Then we want to open this in Sublime Text, and to do that, we say open,
02:18~/.bash_profile -a, and the path of Sublime Text, which for me is
02:25Applications/Sublime\Text.
02:29Now we have to use the Export command to tell our bash profile where the Android SDK is.
02:34We do that by typing export PATH, in all capital letters, equals dollar sign curly
02:41brace PATH and then a colon, and now we are point it to a couple of folders that
02:47are a part of the Android SDK.
02:49Your path may be different, but my Android SDK is in my Applications folder,
02:54under the adt-bundle-mac-x86_64 folder.
03:00If you downloaded it following the instructions in a previous movie, it may be
03:04in a similar place for you.
03:05I am going to copy the name of this folder, go back to Sublime Text, and then
03:12Applications, the name of the folder--sdk--and inside this folder I want
03:19platform-tools. Follow that with another colon, and then I'm going to copy this
03:26entire string, paste it after that colon, and remove the platform-, so it just
03:38says tools. And then I'll save my bash profile.
03:44Those two folders contain a bunch of the SDK tools that the command line tools
03:48that ship with PhoneGap are going to use to create and test projects.
03:53To use this I'll quit Sublime Text and I'll also quit my Terminal and open it back up.
04:00Now I can use those Android scripts that come with PhoneGap to create a project.
04:04So I'll go into the PhoneGap directory, which is still in downloads for me,
04:08phonegap-2.3.0/lib/android/ bin/, and there are the tools.
04:17If you're on Windows, you can use create.bat, but on Mac I'll use create and
04:23then I'll pass in the same parameters that I would if I were on Windows.
04:27First, we need to give a location for our project, and I'll put it on the
04:31Desktop, Desktop/android-project. Then I'll give my project an ID, which usually
04:39takes the form of a reverse domain, so it's unique, com.example.android.
04:45And finally, I'll give my project a name: android_project.
04:49You won't be able to use dashes in the name of the project, so keep that in
04:53mind. Push Return and behind the scenes it's not going to create my Android
04:58project for me on the desktop.
05:01If I go back to Finder and go to the desktop, I'll now see my
05:06Android project with all of the native Android files there that I need to
05:10start building this for PhoneGap.
05:11So now we can open up that Android tool that we downloaded in the previous movie.
05:16Go to Eclipse, open up Eclipse, select the default workspace, and now I can
05:24import that existing Android code into my workspace by browsing to its location
05:32on the desktop. android- project, Open, and Finish.
05:38Now we have the native Android project here in the native development tools for Android.
05:44The next step to get this running is to set up the Android virtual machine, which
05:47I can do by clicking the Android Virtual Device Manager button.
05:51Here we can set up the emulator.
05:53I'll do that by creating a new virtual machine.
05:56I'll give it the name NexusS, which will reflect the device we were planning on
06:02using. Give it a target of Android 4.2 and I'll click OK.
06:10Now we have our virtual device set up.
06:14I can select my projects, go to the Run menu, and Run As > an Android Application.
06:23That's going to compile my Android project, and this will start the emulator and
06:27start your application loading.
06:29This can take a little while, and there the application is running.
06:37We can also use the PhoneGap command line tools to run the emulator from the
06:41command line. Go to the Terminal and go to the place where you created the
06:46project. For me it's Desktop/android-project.
06:52Inside that directory is a Cordova directory which contains a bunch of script
06:56files that we can use to build and test the project.
07:02In this example, we're going to use the Run command, so we just type /run,
07:08and it's going to build and compile the project and then deploy it to our
07:12Android virtual machine.
07:18If you prefer not to use the Eclipse tools, you can use the command line tools
07:22as a more lightweight way and more flexible way to deploy content of the device.
07:27Using the command line tools in conjunction with the tools from the native SDK
07:31provides everything you need to create and customize a PhoneGap application.
07:35Let's move on and get started building our application.
Collapse this transcript
Starting a project with the Windows Phone 8 template
00:00Most of the editing that this course will need will be done in a text editor,
00:04but for compiling and debugging, the native SDKs can provide helpful features.
00:09Let's look at how to get set up with PhoneGap in the Windows Phone 8 SDK.
00:13Start by downloading the latest version of PhoneGap. Then you'll need to browse
00:18to the Windows Phone 8 SDK page and download that as well.
00:23Once those are installed and extracted, go into the PhoneGap directory.
00:28In the lib directory, you'll see Windows Phone 8.
00:32The Window Phone 8 distribution comes with two types of templates.
00:36The full template is a prebuilt DLL containing all of the framework code for PhoneGap.
00:41Using this template will make your project easy to update because you can
00:45replace the JavaScript and the DLL in the project and then rebuild it when a new
00:49version of PhoneGap comes out.
00:51The problem is that the DLL contains all the PhoneGap APIs and will thus require
00:56all the permissions, even ones you don't use.
00:59This can be an issue when you submit your application to the Windows store.
01:03The standalone template includes all of the source code for PhoneGap.
01:06This template is easier to customize to be sure that you're using only
01:10features you need, but will be harder to update new versions of PhoneGap when they come out.
01:15To start off, we're going to copy the full template over to the templates
01:19directory in Visual Studio.
01:21So copy the zip file and browse to My Documents/Visual Studio
01:262012/Templates/ProjectTemplates and inside that folder, paste the zip file.
01:36Next, open up Visual Studio. And now, if we go to New Project, at the top of the
01:46page I have my Cordova Windows Phone 8 template. Click OK and it'll create a new
01:52PhoneGap project for me in Visual Studio Express.
01:56Here's the www folder, and inside that www folder is the CSS, JavaScript, and
02:03HTML that we will be modifying throughout the course.
02:06To test this application make sure the emulator is selected above and press the
02:13green arrow key to start emulation.
02:17That will start the Windows Phone 8 Emulator, and we can see our PhoneGap
02:21project running inside.
02:23Now we have our PhoneGap application running in Windows Phone 8 and we're
02:26ready for development.
02:27With the Windows Phone 8 SDKs installed, you can use the native tools for
02:31debugging and testing.
02:32In addition, PhoneGap can use these native SDKs to package up mobile
02:36applications for app stores and marketplace distribution.
Collapse this transcript
Adding UI features with jQuery Mobile
00:00jQuery mobile is a powerful framework for building mobile web applications.
00:04It is built on top of jQuery, but includes mobile-specific components and
00:08transitions that speed up the process for building an application.
00:11Originally, jQuery mobile was intended for mobile web content, but with the
00:15popularity of PhoneGap, it is often used create mobile web applications as well.
00:19jQuery mobile provides a robust UI library that is touch- and mobile-friendly.
00:24It includes a lot of mobile UI paradigms like pages and transitions as well as touch events.
00:31jQuery mobile also will degrade gracefully, which means that if for some reason
00:36you can't load all the JavaScript or all the CSS files, all the content on the
00:40page is still viewable.
00:41This is less of an issue with applications, but it can still be good to know.
00:46A full explanation of jQuery mobile is beyond the scope of this course, but
00:49you can find other jQuery mobile titles here in the lynada.com online rraining library.
00:55Let's go to exercise files and open up the placeholder document that we created in
00:59earlier movies. So remember, this is just a structure of a document we created,
01:04and we added some PhoneGap-specific content to it so that we could be ready for
01:08the onDeviceReady event.
01:10If you open up your exercise files, you'll see code-snippets.txt, so go ahead and
01:16double-click that. And we are going to copy this and bring that into Sublime
01:24Text and replace our placeholder div with that content.
01:30Now, this gives you a sense of how jQuery mobile is structured. It's divided up
01:34into pages, which will be rendered screens when running on the device.
01:39You can either have your pages in one file, like I have them here--this page
01:43and this page--or you can create separate HTML documents for separate jQuery mobile pages.
01:49If we save this and open it in a browser--and we can do that by dragging over the
01:56index.html page--you'll see that it doesn't really look like an application.
02:00There's not any styling too it. Some content's there.
02:03We have a link here that goes to a hash URL right now.
02:08And what jQuery mobile is going to do is take this document structure and turn
02:12it into something that looks polished and like a regular mobile application.
02:17The first step to using jQuery mobile is to go download the files that you'll
02:21need, which you can do at http://jquerymobile.com. Click on the download link,
02:27and when you get to the download page you'll be given a whole bunch of different
02:30options for ways to incorporate jQuery mobile into your content.
02:35What jQuery mobile recommends is that copy and paste this snippet for CDN-hosted files.
02:39The reason jQuery mobile recommends this is because when jQuery mobile is used on a website,
02:45this can help speed up its loading time. But we have to make sure that the
02:49files are available even when there's no Internet connection, because we are
02:51running an application.
02:53So we want to download the ZIP file that contains all of the JavaScript, CSS, and images.
03:00When that downloads, open up the download location and unzip the ZIP file.
03:06Then double-click on the jquery mobile folder and you'll see a whole lot of
03:12CSS and JavaScript, as well as some folders that include the images that ship
03:16with jQuery mobile.
03:17The folder we want to worry about his demos.
03:20We're going to double-click on demos, and we're going to copy the CSS and
03:25JavaScript folders into our exercise folder.
03:30Go back and paste them with Ctrl+V or Command+V on the Mac.
03:36Inside the CSS and this JavaScript folder are all the JavaScript and CSS
03:40files that we need.
03:41We have jquery, we have the jquery.mobile JavaScript file, and we also have the CSS
03:47files that we can incorporate into our application.
03:49So as the next step, go ahead open up the index.html page that we saved in Sublime Text.
03:55And we're going to bring in those jQuery mobile files and reference them from
03:59here in the document.
04:01The first thing we want to do is reference that CSS style sheet to incorporate
04:05the jQuery mobile theme.
04:07We do that by saying link rel=" stylesheet", and for the href we are going to
04:14point to css/themes/default/jquery.mobile-1.2.0.css, Then, above the
04:28cordova.ios.js script tag or cordova.android.js if you're using android,
04:34you'll want to add the two JavaScript files that we need for jQuery and
04:38jQuery mobile respectively.
04:41So go ahead and do script src= js/ jquery.js. As I mentioned, jQuery mobile is
04:51built on top of jQuery, so we still need to incorporate the jQuery JavaScript
04:55file, so that we can make use of all the things jQuery provides within our
05:00jQuery mobile application.
05:02The second script tag right below that, we're going to add
05:05js/jquery.mobile-1.2.0.js and close that script tag. Now save this and open it
05:21back up in the browser.
05:22Let's go ahead and drag our index.html file over to the browser window.
05:29Now that same exact content, just by adding some CSS and JavaScript files, has
05:34been transformed into something that looks very much like an application.
05:38That link has been turned into a button, and when I click on that button, it'll
05:43give me a nice transition to what will become our Note Detail page.
05:46As a final step we want to go back to Sublime Text and we're going to create a new file.
05:53This is going to be our CSS file that we're going to associate with our content,
05:57and we're going to add some CSS rules.
06:00So let's go ahead and change the plain text version to CSS.
06:05If you have a text editor that supports this, you'll want to do that as well.
06:09Next, we're going to create a class called logo and we're going to give it a
06:14property of text-align: center. And then we'll go ahead and save it
06:19into our exercise folder, in the CSS folder, as main.css.
06:27Go back to the browser window. We can refresh.
06:29Now we have our logo centered, and we're ready to start adding more content.
06:33With jQuery mobile integrated into the application, we now have something that's
06:36starting to feel like a real mobile app.
06:38With the user interface defined, it is time to move on to adding device-specific
06:42features to the application.
Collapse this transcript
Adding other JavaScript frameworks
00:00Because PhoneGap is just HTML, JavaScript and CSS content, it's possible to use
00:05almost any framework, tool, or technology that leverages those languages.
00:09This means as a web developer or designer, you can use the familiar workflow in
00:13building PhoneGap applications.
00:15It also means that your application can make use of the many great web
00:18technologies that exist.
00:20In this movie, I'll take you through how to do just that.
00:23Something that can be very helpful for web content is templating, or reusing
00:27core HTML code, but with some variables that we will change the content inside of the HTML.
00:33There are a number of great templating frameworks like _JS, Handlebars, and Mustache.
00:39I'm going to use Mustache today, because it's one of the most widely used
00:42templating frameworks, and has support for many different languages.
00:45It's also simple and lightweight.
00:47For more complex applications, you may want to look at Handlebars, which is a
00:51superset of Mustache.
00:53So it works with the Mustache templates, but has a few extra features that can
00:57come in handy when creating bigger applications.
00:58The first thing we need to do is download mustache.js, which we can do by going
01:03to http://mustache.github.com.
01:06Mustache is available in many different languages, but the one we're most
01:09worried about is JavaScript.
01:10The easiest way to get mustach.js is simply to go to mustache.js in GitHub,
01:17click on the file, go to the raw version, and then save this into our exercise folder.
01:25And we're going to save it in the JavaScript folder.
01:27Once we've got mustache.js downloaded, let's open up our exercise folder.
01:35Specifically open up index.html. Now to use mustache.js, we first need to
01:41reference that script file.
01:42So below your cordova.ios.js script, source or cordova.android.js if you
01:48are following along for Android, create script src js/mustache.js and close the script tag.
01:57To use mustache.js, you first need to set up a template, which is the code that
02:01will be rendered when we call mustache.
02:04Mustache uses double curly braces to define variables.
02:07Those variables are populated by a data variable that is passed into the mustache call.
02:12In the exercise file, you'll find code-snippets.txt. Open that in a text editor
02:19and you'll see the data, as well as the HTML template that we are going to use.
02:23Copy that, go back to Sublime Text, and at the very top of the script
02:29block, paste it in.
02:36Notice the template file, which includes the HTML I want Mustache to render for me.
02:41Also notice the double curly braces, which are variables that correspond to the data above.
02:46When we call the Mustache template, it's going to look at that data and create
02:50the HTML based on the data source above.
02:52Now let's take a second to talk about jQuery mobile events.
02:55Similarly to PhoneGap, jQuery mobile has an event that triggers when it's safe
03:00to use other jQuery mobile features.
03:03This is called the page init event.
03:05So anytime we want to call something with jQuery mobile, we will put it inside
03:09that page init handler.
03:11On top of the init block, we're going to add an event listener to the document
03:16using jQuery syntax, and we're going to listen for pageinit. And when that
03:23pageinit is called, we're going to do everything within this function.
03:28For this template we want to use the HTML down in the new note page below, and
03:33to do that, we want to listen for the page create event on that object.
03:37So we'll create a jQuery event listener on our newnote page, and set to
03:45pagecreate, and then create the function that will be called when that event fires.
03:51Inside of this function is where we'll start to call our mustache template.
03:55Mustache renders into HTML, so we'll first create a holding object for that
04:00HTML. Then we'll call the Mustache library and the to_html function, which is
04:09going to take that template data and turn it into HTML we can use.
04:12So we'll pass in the notetemplate, as well as the data structure that will be
04:19rendered based on that template.
04:20Once we have the HTML, we want to insert the HTML into the page below.
04:24We can do that with jQuery by selecting the notedetail content and inserting
04:31the HTML we created into the HTML property.
04:34Go ahead and save this file and then load it in the browser by dragging it from
04:41Finder or from your location to the browser window.
04:46Now we have our application, and when we click newnote,
04:49it's going to render the template, so we'll have the other jQuery mobile
04:52components there that can start using.
04:54Now you've seen how easy it is to incorporate other frameworks and
04:57technologies into PhoneGap.
04:59This makes PhoneGap a powerful tool for web developers who want to take
05:02advantage of frameworks or technologies that they already know.
05:05Many of them work to make PhoneGap applications more robust.
Collapse this transcript
Using the Connectivity API
00:00PhoneGap provides access to native device APIs through a JavaScript bridge.
00:05That means all developers need to do in order to use the APIs is write JavaScript.
00:10All the data from the device sensors are passed back to the application through
00:15the bridge, which means developers never have to worry about accessing or using
00:20the native API directly.
00:22For a lot of applications it is helpful to know if the device currently has
00:25an Internet connection or not; that way you can adjust the user experience accordingly.
00:30There is a native API that provides this information, which is bridged through
00:34PhoneGap's connectivity API.
00:37Let's go ahead and open index.html and scroll down to the onDeviceReady event.
00:43If you'll recall, whenever we are calling PhoneGap APIs, we need to make sure they
00:47happen after DeviceReady.
00:49The easiest way to do that is to put calls within the DeviceReady function.
00:55For this example we'll delete the alert, and we're going to create a variable to
01:00store the network state: var networkstate.
01:04Now we can call the PhoneGap API.
01:08Most PhoneGap APIs are on a navigator object.
01:11So we'll type navigator.
01:13Now we can call the connection API, which includes a type string that's going to
01:18return the type of connection we currently have.
01:21This could be WiFi, cellular, or could be none at all.
01:25Once we know the network state, now we want to customize the application based on that.
01:30Down here, at the bottom of my application, I have this Offline mode class.
01:36I don't yet have a CSS style associated with it, so it's always going to show up,
01:40but I have a CSS style that we're going to create later that's going to hide it,
01:43unless we have an offline connection.
01:47So up here onDeviceReady, I'm going to say if(networkstate == "none"), which is
01:54the value that navigator connection object will return if I don't have an
01:57Internet connection. Then I'll create a selector on the offline class that's
02:03going to change my CSS visibility property to visible.
02:08Now let's go set that class up inside of my CSS file.
02:13Go back to the exercise files, go into to the css folder, and double-click
02:18main.css to open it in Sublime Text.
02:21Now, below the logo class, we're going to create offline.
02:26The properties for offline are going to be visibility: hidden; color:
02:32red; and finally font-style:
02:36italic. And save that file.
02:40Now we want this to run inside of the iOS Simulator.
02:44To do that we're going to take all of the files from this Exercise folder--using
02:49Command+A or Ctrl+A on Windows to select them all--
02:52and we are going to copy all of these into the iOS project on the desktop that
02:56we created as a placeholder in the previous movie.
02:58So double-click on ios- project, double-click on www.
03:03I'll select everything in this folder, and then I'll use Command+Delete or
03:08Shift+Delete on Windows to delete them from my project.
03:11Finally, we'll paste our five items from the exercise folder in, move back to
03:17the main level project,
03:20double-click on the Xcode project to open it up in xcode.
03:23Now I can run this in the Simulator by making sure that iPhone 6.0 Simulator is
03:27selected and pushing Run.
03:32And here's our snap!note application with no offline mode down here, because we
03:37have an Internet connection.
03:38But let's see what this looks like without an Internet connection.
03:41Let me go ahead and quit the Simulator, and then I'm going to go to the
03:47Network Preferences.
03:50The iOS Simulator uses the network connection coming from the computer to
03:54determine its own network connectivity.
03:56So in order to simulate offline mode with the Simulator, you have to actually
04:00turn off the Internet connection on your computer.
04:02Other simulators let you have more control over the kind of Internet connection
04:06that's coming into the emulator.
04:07And if you need to disable your Internet connectivity for any reason, you can
04:10also do this on Windows.
04:12On Mac, I'll go ahead and go from DHCP to Off, push Apply.
04:16And now I'll go back to xcode and run my application in the Simulator once again.
04:24This time when it loads, it detects that we don't have an Internet connection
04:27and it's going to display that red Offline mode so that users of the application
04:31know they're currently offline and don't have an Internet connection.
04:34The PhoneGap APIs give you everything you need from JavaScript.
04:37The connectivity API is one of the most basic APIs to use, but it can be very
04:42handy when customizing an application experience.
Collapse this transcript
Using the Geolocation API
00:00Another common API that applications make use of is the geolocation API, which
00:05uses a combination of GPS and wireless to provide a user's location.
00:09This video will cover how to use the geolocation API in PhoneGap.
00:14In order to use geolocation on Android, you need to explicitly call out the
00:18permission in the manifest file.
00:20On iOS, the user will be prompted to allow the application to request their location.
00:25Most devices have two ways to get location.
00:28One is the GPS sensor, which is the most accurate.
00:32This is often called fine location. But many devices also can get location by
00:37looking at the wireless networks around and calling a web-based location service,
00:41which is less accurate.
00:43This is often referred to as coarse location.
00:45Go ahead and open the exercise folder and double-click on index.html to open it
00:51in your text editor.
00:53The first thing we have to do is add the scripts that we're going to call
00:56from Google Maps, so that we can insert a map in our application after we've
01:00called the location.
01:01Open up code-snippets.txt in the exercise folder and copy the first two lines:
01:07the style sheet and the script tag.
01:11Go back to your text editor and right below mustache.js, paste those two lines
01:15of code into the editor.
01:19Normally, when calling PhoneGap events, we want to make sure we call them in
01:22onDeviceReady, but in this example we're going to be waiting for a second page load,
01:27so we're going to put the event in the pageinit function.
01:30Start off by creating an event listener for the newnote page.
01:36And we're going to bind this event listener to the pageshow event. Pass in the function.
01:44Inside this function we're going to call the geolocation APIs.
01:48We do that by typing navigator. geolocation. And the method we're going to call
01:54on the geolocation object is getCurrentPosition.
02:00getCurrentPosition takes two different parameters.
02:02The first one is the function will be called when we successfully get the user's location.
02:07We'll call this onGeoSuccess.
02:11The second is the error function that will be called when something goes wrong
02:15with trying to get location.
02:16This will be called onGeoError.
02:20Scroll to the bottom of the script block and right below onDeviceReady, create a
02:24new function onGeoError.
02:27It takes one parameter, which is an error object.
02:31So if we have an error, we want to tell the user to turn on their
02:37geolocation services.
02:38Now we want to create the onGeoSuccess function.
02:47Go to the exercise folder and open up code-snippets.txt again, and select the
02:53onGeoSuccess function. Copy that. And in between onDeviceReady and onGeoError,
03:01paste the new function.
03:04The Success function takes a single object, position, which will include the
03:07latitude and longitude of the geolocation object.
03:10It is then going to set that latitude and longitude object to a current
03:13position, and this is the code that's going to load Google Maps and display
03:17the map on the screen.
03:19Scroll to the top of the page and add var lat, lon.
03:23This will ensure that later in the course, other parts of the application can access
03:27latitude and longitude.
03:29The last step is to go to the main.css file.
03:33In the exercise folder, under the css folder, double-click on main.css to
03:38open it in a text editor.
03:40And we're going to create a CSS rule for map.
03:42We're going to give it a width of 100% and a height of 200px. Save that.
03:54Save the changes to your index HTML file.
03:58The next step is going to be to copy what's in this directory into www
04:02directory of your project.
04:04Select all of these files, copy them, and on the desktop, open up the
04:11ios-project, or if you're following along an Android or another platform, open
04:15up the folder for that platform.
04:17Go to the www folder, select everything with Command+A or Ctrl+A, delete it, and
04:24paste in the new files.
04:28Go back and open up the project in your native IDE.
04:31In this case, I'll click on Xcode project to open the iOS project in xcode.
04:37When that's loaded, run the application in your Simulator.
04:41In the iOS Simulator and most other emulators, you can change the location
04:45dynamically by setting a preset or adding a custom Location.
04:50In our application when we click New Note, it's not going to prompt us to allow
04:55the application to use the user's location.
04:56Click OK and now the location loads in our Map and we can geotag this note.
05:02Geolocation is one of the first great examples of how mobile applications can
05:06tie into device sensors to create a custom user experience.
Collapse this transcript
Using the Camera API
00:00One of the most common features on mobile devices is the camera.
00:04PhoneGap provides access to camera data in a number of ways, including the
00:08ability to access the photo as a file or get the raw photo data if the
00:12application needs or wants to modify it.
00:15This video will cover how to add camera functionality to an application.
00:19The first thing we're going to do is add the event listener by which we will
00:24call the camera, and we're going to put that in the pageinit function we created earlier.
00:28To add the event listener add a jQuery selector on addphoto, which is the ID of
00:35the button that's going to invoke the camera. Use the bind method and bind it to the tap event.
00:41The tap event is a jQuery-mobile- specific event, and it's often a lot better than
00:46using the click event because tap events are more efficient than click events.
00:50The tap event is a jQuery-mobile- specific event, which is specifically
00:54targeted at mobile devices;
00:56it's more efficient and faster response time to use the tap event, so use it
01:00whenever possible in lieu of the click event.
01:03Second, create the function
01:05that's going to hold the code that we will call when the tap event fires.
01:09The camera API introduces options which allow you to customize how the API call will be made.
01:15In this case, you want to customize two options: the source type and
01:19the destination type.
01:21To do that we'll first to create an object called options, and inside that object
01:28we can specify what options we want to use.
01:31We'll start with source type. Source type
01:33lets you switch between using the camera or using the photo library on the device.
01:38For the purpose of this course, we're going to set the source type to
01:41Camera.PictureSourceType.PHOTOLIBRARY.
01:49The reason for this is on the iOS simulator we can't simulate the camera.
01:53On Android and other platforms you can simulate the camera, so feel free to use
01:58camera here if you'd like.
01:59The second one we'll use is destinationType. destinationType lets you reference
02:05the photo as a file on the file system or as a data URL.
02:09It can be handy to access it as a data URL if you want to manipulate the pixels
02:14and change how the image is going to appear.
02:16But keep in mind, because of high- resolution cameras on the device, using data
02:21URL here could result in a very high memory footprint, so I really, really
02:25recommend that you use file URI instead, which is what we're going to do.
02:29We do that by setting Camera.DestinationType.FILE_URI.
02:37You can see the PhoneGap documentation for a full list of camera options.
02:41Next, let's use the camera API.
02:44We do that by calling navigator.camera.getPicture.
02:51The getPicture method takes three parameters.
02:53The first one is the function that will be called when the camera call is successful.
02:58The second is the function that will be called if something happens and there's
03:01an error with the camera call.
03:03The third one are the options that we just passed in that will help customize
03:07the camera experience.
03:09Let's call the first parameter onCameraSuccess.
03:14For the second one we'll use onError, and the third one will be the options
03:18object that we created above.
03:21Now scroll down to the bottom of the script tag and below on GeoError, create a
03:27new function called onError.
03:29And onError will take a message property, which is just a string that returns
03:35the error message to the user. So we can show them that by saying alert, the message.
03:41Next, scroll to just above the onGeoError and create a new function. Call this
03:48one onCameraSuccess.
03:51This function is going to take an image URI property, which is going to be a
03:55string representation of where the camera data is on the file system.
04:01In the template above that we created with Mustache.JS, we have an image
04:05placeholder that will display the image when it comes back from the camera.
04:09So in this function we're going to set the source attribute of that image to
04:14the image URI that we get back from the camera and we're going to make sure
04:17that it's displayed.
04:19We do that by adding a jQuery selector to noteimage, which is the ID of that
04:24image, and we'll use the attribute method to change the source attribute to the
04:29imageURI from the camera.
04:33Next, we'll do the same selector, but this time we'll change the CSS property
04:39display to block, which is going to show it on the screen.
04:44The last step here is to go into main.css and set some parameters for how the
04:48image will display by default.
04:50So go back to the exercise folders, click on css, and open up main.css in your text editor.
04:56Below the map selector, create a new one for noteimage and set the width equal to
05:03200 pixels, the height equal to 200 pixels, and the display to none.
05:10This is because we don't want the user to see a broken image icon,
05:14so we're going to just hide the image until we've already created and set the
05:17source. Make sure that's saved.
05:23Go back to the main exercise folder and copy all the files to the clipboard.
05:29Next, go to the desktop and open up the iOS project.
05:33If you're following along with Android or one of the other platforms, go into
05:37that project for your platform, go into www folder for your platform, select all
05:43the things that are already there, and either delete them or replace them with
05:47the new objects by pasting over.
05:50Next, go back to the main project file and if you're following along on Xcode,
05:55open up the Xcode project.
05:56If you're using Android or one of the other platforms, open the project in your
06:00native IDE. Go ahead and open the Simulator by running the project.
06:07By default, most emulators and simulators won't have anything in the photo
06:11library that we can use, so we're going to have to populate it ourselves.
06:15On iOS, this is a fairly straightforward process.
06:18You'll have to double-check the instructions for Android or your other platforms
06:21for getting files into the photo library for that specific platform.
06:26If you're following along with iOS, go to the exercise files and select
06:33img27.jpg. Make sure you're on the home screen of the simulator and drag the
06:39image file to the home screen.
06:41It will load that image file in the browser. Click the Share button and click
06:47the Save to Camera Roll button to put that image in our photo library.
06:50Now go back to the home screen and run the project. Select New Note. It's going
06:59to prompt us for our location again. Select the Add Photo button and it will
07:03bring up the photo library.
07:06Notice we're using the native photo library UI.
07:08This isn't something we had to create custom. PhoneGap is using the same native
07:12API, so it's going to use the same user interface for selecting photos. Select
07:18our save photo, and you'll see there it is, rendering as part of our note.
07:23The camera API gives you access to the photos taken in that moment or the photo
07:27library of the device.
07:29It is an easy way to incorporate user-created visuals into an application.
07:33There are a number of options that we will modify how the camera is called and
07:37what kind of data is returned to the application.
Collapse this transcript
Using the Storage APIs
00:00Most mobile applications will want to be able to store information offline.
00:04PhoneGap offers two ways to store data, and both track the W3C's web storage APIs.
00:11The first lets developers use SQL syntax to save information to a database that
00:16can be called upon later.
00:17The second uses a set of name-value key pairs to store information.
00:21Go ahead and open up the exercise files in Sublime Text, and open index.html.
00:27To use the storage APIs on Android, you have to make sure that you request
00:31storage permissions in the manifest file.
00:33On iOS no permission is required.
00:35The first thing we're going to do is create some variables that we will need that
00:39will give us access to the database throughout the application.
00:42So in the first line right below your initial script block, create db,
00:46dbresults, and itemindex.
00:49In order to use this storage APIs, you have to create a database that will be a
00:55file on the device itself.
00:57So scroll down to the onDeviceReady event. Inside that event start with a
01:03variable for the database, db, and we're going to call window.openDatabase.
01:10This is the function that's going to create a file for us.
01:14It takes four parameters.
01:15The first is the name of the database, and we'll call that Notes.
01:19The second is the version of the database.
01:22Go ahead and give that a version of 1.0.
01:24The third is the display name for the database, and call that Saved Notes.
01:30Finally, set the size for the database.
01:33This one won't require a large database, so we'll do 200,000 bytes.
01:39In order to perform transactions on the database, you use the transaction method
01:44on the database object.
01:45So use db.transaction, and this is going to take three parameters.
01:51The first is going to be a function that will contain the SQL calls that we are going to make.
01:56Call this getDbNotes.
02:00Second is the function that will be called when an error happens with the
02:04transaction. For this call onDbError.
02:07And finally is the function that will be called when the transaction is successful.
02:13We'll call this onDbSuccess.
02:17Notice that the order in the transaction method is different from some of the other APIs.
02:22In this one, the error comes before the success callback.
02:25Now, scroll down to the bottom of the script tag, right below the onError
02:30function, and we're going to create our database error handler.
02:34So create function,
02:35call it onDbError, and it's going to take a parameter of error.
02:42When we hit an error we want to alert the user to the issue, so we'll call the
02:45alert tell them they have a Database error and provide them with information
02:51about the error message, like printing out error.message.
02:56Now, let's create the DB success method.
02:59Scroll up to right below onCameraSuccess and right above onGeoError, and create a
03:06new function called onDbsuccess.
03:10This function takes two parameters.
03:12The first is the transaction.
03:14The second is any results that may come from the database transaction.
03:18For this method we don't need to alert the user that it's been successful, but
03:22it's helpful to know from a developer standpoint what's happened,
03:24so use console.log and just leave the note that says the Database call was successful.
03:32To get DB notes is what's going to contain the SQL statements that we want to
03:36use to call against the database.
03:38So, right below DeviceReady and above onGeoSuccess, create function get DbNotes.
03:46This takes one parameter, the transaction, because we're going to write the SQL
03:50statements to that transaction object.
03:53We'll pass that in as tx.
03:55To execute SQL statements, we use the execute SQL method on the transaction.
03:59So type tx.executeSql, and now we can start typing our SQL statements.
04:06For the first one, we're going to create the table if it doesn't already exist.
04:10We do that by using he SQL CREATE TABLE IF NOT EXISTS, and we'll use the table name NOTES.
04:19Then we'll give this table 5 columns:
04:23notetitle, notedetail, imagesource, notelat for note latitude, and notelon for
04:34note longitude. And finally, close the parenthesis.
04:38The next SQL statement we want is going to select any potential records from the
04:42table so we can display those if we've already saved notes offline.
04:46To do this, we'll call executeSql again on the transaction, and for the SQL
04:52statement we're going to call SELECT * FROM NOTES.
04:57In this SQL statement we're also going to pass some additional parameters.
05:01In this case we're going to start with an empty array, which would normally be
05:05used if we want to pass parameters into our SQL statement directly.
05:09The third parameter is going to be the function that gets called once the SQL
05:13has executed successfully.
05:15For this, we'll call onSelectNotesSuccess, and finally, the function that will
05:23be called if there's an error--and for this you can reuse onDbError.
05:29Once the notes have been selected from the database, the goal is to display
05:32those on the front page as a list of saved notes.
05:37Go ahead and go to the exercise files and double-click on code-snippets.txt to
05:41open it your text editor.
05:45Select the onSelectNotesSuccess function and copy it.
05:50Now go back to text main, and right below the getDbNotes function, paste in this code.
06:00This function also takes two parameters: the transaction and results.
06:04And the first thing that we're going to do is save the results to the
06:08global dbresults variable
06:09so we can refer to it through the rest of our application.
06:13The second thing it does is sets the number of results to a temporary length variable.
06:18Then it loops through the length variable, and for each record in the list it's
06:23going to create an item in the list.
06:27Finally, we call a jQuery function listview "refresh" that's going to force
06:31jQuery mobile to rerender the list with our items.
06:35The next step is to create the insert statements so that we can save notes to he database.
06:41Go back to the exercise files and open up code-snippets.txt again.
06:47Copy the savenote bind function and go back to Sublime Text.
06:56Scroll up to the pageinit function and inside that function at the bottom,
07:02paste the code from the snippets file.
07:07This code is going to bind a tap function to the savenote button, and when that
07:12tap event happens, we're going to pull the values from our forms and set those
07:16to the notedata, so that we can save them in the database.
07:23Finally, we call a transaction on the database that's going to save our note, and
07:27we'll use the saveNote function to create that SQL.
07:31Open up the snippets file once again by going to the exercise file and
07:34double-clicking on code-snippets.txt.
07:38Select the saveNote function and copy it.
07:42Go back to Sublime Text and right below onSelectNotesSuccess, paste the saveNote function.
07:52For saveNote, we're going to call an executeSql method on the transaction.
07:57That executeSql method has the insert SQL statement that we're going to use to
08:02save the note to the database.
08:04Notice here in the SQL statement that we use question marks for values.
08:08In this case we're going to replace those question marks with an array of our notedata.
08:14The next step is to make sure that when we load an old note into the note detail
08:17page that we will see the updated values.
08:20Go back to the code-snippets.txt file and copy the last code snippet, which says
08:29newnote live "pagecreate," and copy that.
08:34Go back to Sublime Text and scroll up to the pageinit function.
08:39We're going to select the old newnote function where we introduced our Mustache
08:44template, and we're going to paste in the new value.
08:51This function is going to first look at itemindex to see if it's greater than 0.
08:55If we click on a note, the code in that link tag will set the itemindex so
08:58that we know it exists.
09:00After that, we're going to grab an individual item based on the item index from
09:04the database results that we saved.
09:07Finally, we're going to pass in the notedata from the results into the
09:11notedata object that the mustache.js template is going to use to parse and
09:16load the application.
09:20Finally, we have our original Mustache code
09:23that's going to turn that into HTML that we can insert into the DOM.
09:29The last step is to make sure the image shows up, and we have to do that within
09:34the pageshow method.
09:35So below the geolocation call, add an if statement to see if the itemindex is
09:40greater than or equal to 0.
09:42If it is, we're going to use a jQuery selector to call noteimage and change the
09:50CSS property of display to block.
09:54Go ahead and save index.html.
09:57Now, the very last step is to do a bit of visual cleanup,
10:00so go into the exercise files and select everything except for code-snippets.txt
10:05and copy all those items.
10:07Go to the Desktop, or wherever you are keeping your projects, and double-click on
10:11a project that you are using.
10:13Go into the www folder and replace the contents of that folder with the items just copied.
10:19Now go back to the main part of the project and open the Xcode project if you're
10:26following along for iOS.
10:27If you're following along for Android, open the project in your native IDE.
10:33Run the project in a Simulator, create a new note, add some detail, add the
10:48photo, and scroll down and click Save Note.
10:54You'll see down in the console that the call was successful.
10:58Exit the application by clicking the Home button or do whatever you need to
11:01do on your platform.
11:04Double-click the Home button to make sure the application is closed, stop the
11:10application, and run the simulator again.
11:12There is the note, pulled from the database, and clicking on it gives us the
11:19details and the information of the note that we have saved.
11:22That provides you a look at how you can save information to a database using
11:25the localStorage APIs.
11:27For more basic information you can also use a localStorage API.
11:32LocalStorage lets you use a set of key-value pairs which you can create by doing
11:36windows.localStorage.setItem.
11:40You can then retrieve data either by key or by index with a getItem call.
11:46In general, the SQL APIs are good for more complex data, while localStorage can
11:51be handy if you have basic data that you want to save very quickly.
11:55That's a look at the two easy ways to store information offline.
11:58Both of these will persist so the next time your application opens, all the data
12:02stored will be saved and ready for your users to use.
Collapse this transcript
Exploring other APIs in the PhoneGap documentation
00:00We looked at some of the core APIs in PhoneGap that you'll use when building
00:03PhoneGap applications.
00:05But there are also a lot of other APIs that will come in handy while
00:08working with PhoneGap.
00:10The best place to get information about those is the PhoneGap documentation,
00:14which can be found at docs.phonegap.com.
00:18The first thing to be aware of is in the upper right-hand corner, you'll see a
00:21version number for the PhoneGap documentation.
00:24You can go back and look at old versions or you can specify the version that you'd like.
00:28You want to make sure that the version of documentation you're looking at is the
00:32version of PhoneGap that you're using.
00:33Let's drill into this specific API, the Contacts API.
00:38The first thing you see is a description of what the API does.
00:41In this case the contacts object provides access to the device contacts database.
00:46Then you'll see an overview of the methods, arguments, and objects that you'll
00:50need for this application.
00:52The first content you encounter when using the documentation is permissions.
00:56This shows the permissions that you'll need to enable in the configuration file
01:00or in your project for the different platforms.
01:04Next, let's look the documentation for a method--
01:07in this case the create method.
01:08You get the information about what the method does, the supported platforms,
01:13because not all APIs are supported on all platforms, as well as a quick basic
01:17example of how to use the API.
01:20That will follow with a full example that will be a more robust pluggable
01:24example that shows you how to use the API and how to handle the events.
01:30For some operations, you'll also get a list of quirks for different platforms.
01:34In some cases the APIs behave differently on different platforms, and if they do,
01:38those quirks or those differences will be listed here in the API docs.
01:44At the bottom, you'll see some of the objects that you'll encounter when using the APIs.
01:48For instance, the error objects will have descriptions for what the properties
01:52are, as well as some of the constants, so when you get error codes, you can come
01:56back to the API docs and see what those error codes mean.
01:59Let's take a look at some of the other APIs that you'll find useful.
02:03The Device API provides access to information about the device, like the
02:07platform, the model, or the name.
02:09This information can be useful when you're trying to create a customized
02:12experience based on which device the user is on.
02:17The File APIs give you full access to the file system on the device and lets you
02:21read and write files.
02:24The Media APIs provide a way to record and play audio on the device.
02:29And finally, the Capture APIs provide a robust way to hook into the video,
02:34camera, and audio recording features of the device.
02:37Now that you've seen some of the APIs hands-on and have seen how the
02:41documentation is structured, you should have enough information to extend the
02:44application to do anything you like.
Collapse this transcript
5. Compiling a PhoneGap Application
Use build.phonegap.com to compile binaries
00:00One of the benefits of PhoneGap is that it's possible to target multiple mobile
00:03platforms with much of the same code.
00:06Unfortunately, when doing that, it can become time consuming to install and
00:10use the native SDKs.
00:13As a solution to that problem, Adobe has a service called PhoneGap Build,
00:16which will package PhoneGap applications into multiple platforms without the
00:21need for native SDKs.
00:24To use PhoneGap Build, go to build.phonegap.com.
00:28You can sign in with your Adobe ID or with a GitHub account.
00:32If you don't have an Adobe ID, you can register for a free one by clicking the
00:36Register button on build.phonegap.com.
00:40Click Sign in and enter your Adobe ID credentials.
00:50PhoneGap Build has two different ways to upload an application.
00:54The first is by using a Git repository.
00:57You can paste in the URL of the private or open source GitHub repository and it
01:01will create an application based on that code.
01:03You can also upload a ZIP file directly to PhoneGap Build.
01:08Go ahead and go to the exercise folder and you'll notice that we have a
01:12couple of new files.
01:14In the img directory I've added icon and logo PNGs.
01:17There is also a config.xml file.
01:22Open the config XML file with Sublime Text or your text editor.
01:27PhoneGap Build uses the configuration XML file to set some of the properties for
01:31the application once it is uploaded to the service.
01:34In configuration, you can set the name, description, author information, as
01:40well as preferences.
01:41We've been using PhoneGap 2.3, but PhoneGap Build doesn't always support the
01:46most recent version of PhoneGap, so in this example we're going to use the most
01:50recent version we can, which is 2.2.
01:52I also we have a preference set for webviewbounce, which is an iOS-specific
01:57preference that makes sure the user can't scroll the screen and bounce it.
02:01Lastly, we can define icons and splash screens here in the configuration file.
02:07In this particular example, I've got one for each, but config.xml supports
02:11multiple sizes if you're creating an application that needs to target multiple
02:15devices or different form factors like tablets and smartphones.
02:18The PhoneGap Wiki has a couple of very helpful resources for determining how big
02:24your splash screens and icons need to be for different platforms.
02:31Go back to the exercise folder and open up index.html.
02:36One thing we should change is to make sure that cordova JS is not
02:38being referenced to a specific operating system, so delete the .ios
02:42and save index.html.
02:44PhoneGap Build will just replace whatever version of cordova you're using with
02:48that cordova JS file.
02:50Go back to the exercise files and select everything in that folder and zip it up
02:55using your built-in zip functionality on your platform.
02:59Name that ZIP file phonegap-build.zip.
03:05Now go back to PhoneGap Build, click the Upload a .zip file, and
03:10select phonegap-build.
03:11PhoneGap Build is going to take that HTML, JavaScript, CSS and now package it
03:18into an application.
03:19Once it's finished, click the Ready to build button.
03:24Now, PhoneGap Build is going to start the process of packaging an application
03:27for all the platforms it supports, which currently is iOS, Android, Windows
03:32Phone, Blackberry, webOS, and Symbian.
03:37You can also click on the application name to get more information and change
03:41some of the parameters.
03:43We can see a list of the builds and the status.
03:46And we can click on Settings to see some of the settings that we've established
03:50for our application.
03:53There is the icon and there is the app title and the package.
03:59Notice we can't change these from this screen because we're using the
04:02configuration XML file.
04:04If you don't use a configuration XML file, you can make changes to your
04:07application's configuration here within PhoneGap Build.
04:11In PhoneGap Build I can also define collaborators, or people who can help me
04:15test and deploy my application.
04:17If I click on the Collaborators section, I can add a collaborator via
04:20their email address.
04:21I can choose between two roles.
04:23The Tester role gives the user access to download the application, or a
04:28Developer who can make changes and upload new versions of my application.
04:32I can also update the code by clicking the Update code button, and I can just
04:36send a new ZIP file which will replace the old code with my new ZIP file.
04:40Finally, I could also choose whether people can download this application
04:44without being approved collaborators.
04:46If this is not checked then anyone who has the URL can download the application
04:51and install it on their phones.
04:52Throughout the application I always have access to the QR code.
04:56This QR code links to a page where I can download the binaries for each platform.
05:01You can use your smartphone or other device that supports barcode reading to go
05:04directly to that URL and download the application on your phone.
05:09You will notice that iOS currently has an error.
05:12Some platforms require me to sign my application before I can create a binary.
05:17To sign the application go through the signing process for your platform then
05:21select the Key dropdown and click add a key.
05:25Here you can upload the key so you can associate that with the application.
05:30PhoneGap Build lets you target multiple mobile platforms for the same application.
05:34You can upload your application as well as configure it and PhoneGap Build
05:37handles the packaging for you.
05:39In the end you have the binaries for the major mobile platforms and you can take
05:43these binaries and put them on various app stores and marketplaces.
Collapse this transcript
Installing a binary on a device for testing
00:00Once you're ready to deploy to a device for testing, there are a couple of
00:03ways you can do so.
00:05One is to use the native tools for the platform.
00:08For IOS and Android there are a couple of great courses here in lynda.com
00:13online training library.
00:15I suggest chapter 1 and 2 from iOS Essential Training, with Simon Allardice.
00:21For Android, I suggest Android App Development with Java Essential Training, with Lee Brimelow.
00:27You'll find helpful information in chapter 13.
00:31Secondly, you can use PhoneGap Build to create binaries.
00:35To use PhoneGap Build to log in to the service. This is the application we used
00:40when we first introduced PhoneGap Build. To deploy to some platforms you need to
00:44go through the signing process.
00:46I'm going to going to use IOS as an example because it requires a signed
00:49certificate before it can create a compiled binary.
00:52But the steps are the same for other platforms but require keys
00:56during distribution step.
00:57Click in the red iOS button. Next to iOS,
01:03you'll see No key selected. Select that drop down and click on Add a Key.
01:10In order to submit a key to PhoneGap Build, it needs to be in the p12 format.
01:15For your platform you may have to see how to convert the key to p12.
01:19For iOS and MacOS 10, go to the Keychain, select Keys, and then select the key
01:28with the developer certificate.
01:29I'll have an arrow next to it. Right-click the developer certificate and
01:34select Export. You'll get a dialog box where you can save the exported key. Call
01:41it ioscert, and save it to the Desktop.
01:46Make sure the file format is the p12 format and click Save.
01:50You'll have to enter a password for the key, and you'll have to remember this
01:53password when you build for PhoneGap. So you'll want to make it something you can
01:59remember or write it down so can keep it later.
02:01You'll be prompted to enter the password for your user or computer, and then
02:08the p12 certificate will save to the desktop. Close Keychain Access, go back to PhoneGap Build,
02:16and enter a title for this particular Key. I'll call it iOS Developer Key. Under
02:23the certificate p12 file, click the box, go to the Desktop, and select your p12
02:29file. Do the same for the provisioning profile.
02:34I've gotten this provisioning profile from the iOS Developer portal.
02:37You'll have to make sure and grab yours and download it to somewhere where you
02:41can save it to PhoneGap Build.
02:42Once you've submitted the p12 file and the provisioning file, submit the key.
02:49PhoneGap will prompt you to unlock the iOS Developer Key, so use the password
02:53that you set when you created the p12 file.
02:56You could also choose to set it as your default key.
02:59PhoneGap Build will store your developer keys for one month unlocked. After that
03:03one month you will have to enter the password again. Submit key and the iOS
03:07platform will start to build along with the rest of the platforms.
03:12Once iOS has Built successfully, you can use a barcode scanner on your phone to
03:17go to the QR code and download the application.
03:20One thing to make sure always clicking on settings, and make sure that Only
03:24approved collaborators is unchecked, so that anyone who has the URL, or in this
03:28case the QR code, can download and test the application.
03:32The other option is to keep this checked and before you can download it to your
03:35phone, you'll be prompted to log in to PhoneGap Build on your device.
Collapse this transcript
6. Extending PhoneGap
Introduction to PhoneGap plugins
00:00While PhoneGap has a lot of functionality built-in,
00:03there are a number of plugins that let developers extend PhoneGap's core feature set.
00:07These plugins provide native code as well as JavaScript, and work seamlessly
00:11within PhoneGap applications.
00:13PhoneGap Includes support for most of the common device APIs and features,
00:17things like geolocation, camera--all things that are common across most of
00:21the device platforms.
00:23In some cases there are platform- specific features or other functionality that
00:27isn't supported widely enough to ship by default with PhoneGap. A good example
00:31is the gyroscope on iOS.
00:34iOS is the only platform with gyroscope support, so there is a plugin for IOS
00:38that supports gyroscopes.
00:39For these cases, PhoneGap includes an extension API so developers can write
00:44their own plugins.
00:45A PhoneGap plugin is simply a set of native code that ties into native
00:49functionality on the device, as well as a JavaScript file that exposes that
00:53native code through to the PhoneGap application.
00:56So even when developers are using plugins, they are still only running
01:00JavaScript don't have to worry with native code behind that gives the
01:04plugin functionality.
01:05Because of this, plugins are platform- specific; that means the same plugin
01:09won't work on iOS as on Android.
01:11So if you going to use plugins you need to make sure that the plugin is going to
01:16be available for all platforms you want to target.
01:18One example of why you would want to use plugins include native SDKs.
01:22Companies like Twitter and Facebook spend a lot of time releasing native SDKs
01:26that hook into their services.
01:28With PhoneGap plugins you can use those native SDKs, but then expose them
01:33through JavaScript. So you get all the same functionality within your
01:36PhoneGap application.
01:37There is a library of PhoneGap plugins available on Github.
01:41If you go to the PhoneGap Repository, under PhoneGap plugins, you'll see a
01:45folder for all the major platforms that PhoneGap supports.
01:49Within those folders are specific plugins that are written with the native
01:52code and JavaScript interfaces for those devices.
01:56The PhoneGap core API is focused on features that are generally common to all platforms.
02:01Plugins combine native code with JavaScript to provide developers access to
02:05some additional features on the device that are platform-specific.
02:08Plugins are written for specific operating systems, so in order to use them,
02:12developers need to get the plugin for every platform being targeted.
Collapse this transcript
Using a plugin
00:00A great example of when to use plugins is dealing with the Keychain on iOS.
00:05It's generally not secure to store sensitive information like passwords inside of
00:09local storage, because local storage isn't secure.
00:13iOS has a keychain specifically for that purpose.
00:16It's a secure encrypted database that can only be accessed via its API.
00:21Luckily, there is a PhoneGap plugin that exposes the API to JavaScript, so
00:26PhoneGap developers could access the iOS keychain.
00:30Android also includes a secured keychain, but at this time there's no plugin for it.
00:35The steps to install these plugins will be generally the same across platforms,
00:38though I am going to use iOS.
00:41In this case I'm going to download the Keychain plugin on GitHub.
00:45Just click the ZIP file to download the source for the plugin.
00:50As of 2.3 PhoneGap, has introduced a standard for creating and managing plugins
00:55using a plugin.xml configuration file.
00:58The process for installing these plugins is easy, but requires a few steps to set up.
01:03The first step is to download and install Node JS.
01:08We hit nodejs.org, click the Install button.
01:12It should download node for your platform.
01:14If you need other platforms, you can click the Downloads link.
01:17It will show you all the platforms that node.js is available for.
01:22Go to the location where you downloaded node JS. Double-click on the Package
01:28and start the install process.
01:30In addition to node JS, it's also going to install a package manager called npm.
01:35We are going to use this package manager so be aware of the install location
01:39while node JS is installing.
01:43Walk through the install process, and when it's finished, remember to keep in
01:51mind where npm is installed.
01:55The next step is to download plugman, plugman is available at
01:58github/imhotep/pluman, and it's going to help us manage PhoneGap plugins.
02:04Click on the ZIP link to download the source code for plugman.
02:10Once it's downloaded, go to the install location and extract both plugman and
02:16the KeychainPlugin. Then open up a Terminal window.
02:21To install plugman we are going to use npm. Browse to the directory where you
02:26extracted plugman, and type sudo npm -g install. And that will install plugman,
02:36so you can use it from any directory.
02:38The next step is to install the plugin we want to use into our project.
02:42If you go to the exercise files on the desktop, in 6.2, you'll see a plugin-iOS-desk project.
02:51We want to copy this project and paste it onto the desktop.
02:57In general, Xcode projects don't like spaces in their folder structure, so it's
03:01best to put the project in a place where there's no spaces. Go back to the
03:05Terminal window and go into the project directory.
03:11Now, the way to use plugman is first to specify the platform, then tell plugman
03:16which project you want to install the plugin into, and finally, tell it which
03:21plugin you want to install.
03:22In this case, we will type plugman-- platform ios --project, is the directory we
03:31are in now, so just . And finally, --plug in and the location of the
03:38KeychainPlugin, which for me is Downloads/KeychainPlugin-master. Then push Return
03:46and its going to install the plugin into that project.
03:51You can close Terminal, go into the plugin-ios-project on your Desktop, and
03:58double-click on ios-project.xcode.
04:00When Plugman install out plugin, it created the keychain.js file for us and it
04:08also added the plugins to the plugin directory.
04:12In addition, it added Keychain to our configuration XML file, so they will able
04:18to use it from our PhoneGap application.
04:20Go to of the exercise files, double-click on www, and open up index.html.
04:28This project is a very basic project that uses jQuery mobile.
04:31We have a text input for password and we have two buttons, one that's going to
04:36savepassword and the other that's going to getpassword back for us.
04:40The first step is to make sure keychain.js is part of our application, so we
04:45will add that to the script block, keychain.js, and close the script tag.
04:55The next step is to create two event handlers for the savepassword button and
04:58the getpassword button.
04:59So, do that using jQuery syntax and say savepassword. And in this case we are
05:08going to bind it to the click event and create a function.
05:11We will do the same thing for getpassword: bind it to a click event and create a function.
05:21Now, to use the plugin after we have made sure that keychain.js is part of our
05:26script tag, we also need to create a variable reference for it.
05:29Do this by going to the top of the script block and saying var kc =
05:33cordoba.require, and the path to the plugin is going to be
05:39("cordoba/plugin/keychain").
05:40Now that we have our reference, we can start writing against the KeychainPlugin.
05:48Then in the savepassword function, type kc for our keychain, and we are going to
05:56use the method setForKey.
05:57setForKey takes a number parameters. The first one is the function that will be
06:03called and when we successfully save something to the keychain, and we will
06:06call this onKeySuccess.
06:07The second one is what will happen when something errors out and isn't successful.
06:14We will call that onKeyError.
06:17Next, we set the name of the key that we are going to store, and we will call
06:23this ("examplekeyname"). Next is the name of the service that's going to be
06:28associated with this key, in this case exampleservice. And this so that you can
06:33store multiple keys per service.
06:36Finally, we have to pass in the password that we want to save in the key store.
06:41To do this I'll use a jQuery selector to get the value of the password text box.
06:48The next step is to create the function for the getpassword button, so go
06:53into that function block, type kc for the Keychain, and the function we are
06:57going to use is getForKey.
06:59getForKey takes four parameters.
07:02The first one is the success call, which will be onKeySuccess.
07:07The second is our error function, onKeyError. And then we pass in the name
07:13of the key and the name of the service. In ours this is examplekeyname and exampleservice.
07:22The last step is to create our two event handlers, so go to the bottom of the
07:27script block and type function onKeySuccess. And this takes one parameter, just
07:33to (success) object. And that's going to be a string, so we can alert the user by
07:40passing it into an alert function (success).
07:43Second, we want to create the error function, so function onKeyError, and that
07:50also takes one parameter, an (error) string which we can alert back to the
07:55user. Save this and go back to your xcode project, double-check to make sure
08:04that everything got changed, make sure the simulator is selected, and run this
08:11project in the simulator.
08:12Now we can type in our password and when we are finished, we will click the Save
08:25Password button, get the OK that we've been successful, and we can click Get
08:31Password to get back our password string.
08:34One of the handy things about using the Keychain is this data will even persist
08:40after the application has been deleted.
08:42If I delete the application and then run it again, and simply click to Get
08:54Password button, it's going to retrieve my password using the keyname and the servicename.
09:00The Keychain plugin provides a great example of how you can blend native
09:03functionality and expose that to PhoneGap in JavaScript and HTML.
09:07The Keychain plugin provides a perfect example of why you would want to expose
09:11more native functionality to PhoneGap.
09:13By leveraging plugins, you can create a deep mobile experience without having to
09:17leave HTML, JavaScript, and CSS.
Collapse this transcript
7. Testing and Debugging an Application
WEb Inspector REmote (weinre) basics
00:00Debugging is arguably the most important thing that developers need to be able
00:04to do when creating applications.
00:06And when building mobile applications, nothing comes close to testing and
00:09debugging on the device for getting real-world data.
00:13When using PhoneGap, developers can use weinre, or WEb Inspector REmote, to test
00:17and debug content directly on the device.
00:19Weinre is built on top of Google Developer Tools.
00:23So if you've used the tools in something like Chrome, you'll be very
00:26familiar with the interface.
00:27Weinre act as a server, and when the device connects to it, it exposes the
00:31Developer Tool features to that connected device.
00:34Because of this, you need to make sure that the device can access the computer
00:38that weinre is running on.
00:40To get weinre, the first step is to download node JS.
00:43Once node JS has been installed, open a Terminal window.
00:47On Windows open a command prompt.
00:49On Mac, you should be able to type npm and use the node package manager.
00:54On Windows, you have to browse to the location where you installed npm.
00:58Type sudo npm -g install weinre.
01:04The sudo command is something you'll only need on Mac, and it gives you
01:07administrative access to the command you're running.
01:09On Windows, ignore sudo and just type npm -g install weinre.
01:15Push Enter, type your user password, and npm will go out and grab all the
01:20packages that are needed for weinre.
01:22Now you can just type weinre to start the server.
01:25The server will start at localhost port 8080.
01:29Browse to localhost port 8080 and you'll see the web inspector remote website.
01:34This site has all the information you need about how to use weinre.
01:38The two most important pieces are the debug client user interface and the target script.
01:44The way weinre works is you take the script tag and you add that to your
01:48PhoneGap application, you load the debug client user interface, and then when
01:52your PhoneGap application connects to it, you'll be able to debug and perform
01:56remote functions on the device.
01:58Copy this script tag and open a window to the debug client user interface
02:02by clicking the link.
02:04Now go to the Desktop where we have our PhoneGap projects.
02:07Select the platform that you've been working on.
02:09I'm going to select iOS, but this will work for any of the platforms that you've
02:12been following along with.
02:14Open the project in your native editor and when it loads, open index.html.
02:22Right after the Google Map style sheet push Return and insert the script tag,
02:26save the document, and run the simulator.
02:35When the application loads, hide Xcode and go to the browser where you have the
02:41debug client interface running.
02:43If the target is in green, that means everything working correctly.
02:46Normally, this will be running on the device itself, so you'd be holding the
02:50device in one hand and you can inspect on your machine.
02:54Example, we're going to resize the browser and we're going to have the simulator
02:57side by side, so we can see what's happening.
03:00To use weinre, first click on the Elements tab.
03:03This will give you a snapshot of all the HTML that's running on the device.
03:07As I hover over HTML elements, those elements will be highlighted on the device.
03:18I can drill down and I can get more and more granular.
03:21Within weinre, I have the ability to change content on the device dynamically.
03:25So in this span class, I can double- click on New Note and I can change this to
03:29Add New Note, to see how that looks.
03:33I can also change CSS properties.
03:34If I wanted to see how this font looked in a different color, say red, I can
03:40double-click the color property and change the color to red, and it will update
03:46automatically in weinre.
03:48I can also see the resources that are used-- there is the Notes database--and I
03:54can see things that happen in the console, like console log events, and I could
03:58even run my own console commands.
04:00So for instance, if I want to see the output of the jQuery selector that's
04:04going to pull the New Note page, I can just say console.log, use the jQuery
04:10selector for newnote, push Enter, and it's going to bring back the console log for that element.
04:18Weinre lets developers debug and test content on the device.
04:23Using weinre makes it possible to see the console output, change CSS on the
04:27fly, and execute JavaScript directly on the device in the context of a
04:31running application.
04:32It is a vital tool for anyone building PhoneGap applications.
Collapse this transcript
Testing the application in the Ripple emulator
00:00Blackberry offers a Chrome extension that helps debug PhoneGap applications in
00:04the browser called Ripple.
00:05Using Ripple, you can emulate device-specific features in the browser.
00:09Normally, Ripple works best if you are hosting your content on the server, which
00:13could be a local server on your computer.
00:15But with just a bit of extra work, you can also use Ripple with a file on the file system.
00:21Start by opening up Chrome and go to the Extensions menu by
00:24clicking Window > Extensions.
00:27The first thing you'll have to do is allow access to file URLs in Ripple.
00:31Now close Chrome and open up a Terminal window.
00:34We're going to open chrome from the command line, giving it a special switch that
00:38enables file access to files.
00:41This is how the command looks on Mac.
00:43The switch that we're going to use is the --allow-file-access-from-files switch.
00:48On Windows your location for chrome will be different, but the switch will be the same.
00:53Push Enter and it will load Chrome with that enabled.
00:58Next, go to exercise files and double-click on folder 07_01, then drag
01:04index.html to the browser.
01:08When that loads, in the upper right- hand corner, click the blue Ripple icon and
01:13say Enable Ripple Mission Control.
01:17The first thing you'll have to do is accept the EULA, and then you'll be brought to the
01:21screen or you can start emulating device features from the browser.
01:25From the Ripple emulator, I can change the device, so I can see how the device
01:29will look on, say, an iPhone4. I have information that's being pulled from the
01:34configuration file, so the icon and the name,
01:37and on the right side, I have access to some device-specific settings.
01:41At the bottom, I can see device-specific events, like deviceready or button events.
01:48It's also going to validate my configuration file so I can see if I have anything
01:52in the configuration file that's not valid.
01:55And near the top, I can start to tweak some of the device-specific settings, like
01:59the network status or geolocation.
02:03In this case the geolocation is currently set to Waterloo, which is where RIM is based.
02:09So if I click New Note in my application, it's going to pull the geolocation for
02:13that Waterloo location and geotag my note with the location from Ripple.
02:17The Ripple emulator is a fantastic tool,
02:19and you can use it for your PhoneGap applications, even if you're not
02:22targeting Blackberry.
02:23It provides access to device features in the browser so is a quick and easy way
02:28to test some of that functionality before deploying it to a simulator or device.
Collapse this transcript
Testing and debugging with PhoneGap Build
00:00PhoneGap Build includes a hosted version of weinre called debug.phonegap.com.
00:05This allows developers to use weinre without setting up their own server and
00:09making it accessible to the device.
00:11Applications built with PhoneGap Build can be configured to
00:14use debug.phonegap.com.
00:16If you prefer not to use PhoneGap Build, you can also use debug.phonegap.com by itself.
00:22Simply put your browser to debug.phonegap.com and you'll see a very simple
00:28Getting Started screen.
00:30The first thing you will have to do is select a unique ID.
00:34This is because debug.phonegap.com is available to anyone on the Internet.
00:38For my ID I'll use ryan12349876.
00:42To use this I'll first click this link to start debugging, and I'll go ahead and
00:47open this in a new tab
00:48because I'm going to select this script source, copy it, and I'm going to paste
00:55this into my PhoneGap application project.
00:57To do that I'll go to the desktop.
00:59I'm going to double-click on the ios-project.
01:00The steps will be the same for whichever platform you're following along with, so
01:05just open up your native platform project in the native IDE.
01:08I'll double-click on xcodeproj.
01:12This should be the same application
01:13we just looked at for using weinre as a local server on our machine.
01:18So to use debug.phonegap.com, all I have to do is select the script, which is on
01:23line 16, and paste the new version that references debug.phonegap.com instead
01:28of a local host weinre version.
01:30Save the script, make sure you're running in the Simulator or whichever
01:33environment you plan on using this on, then click Run.
01:39Now, switching back to debug.phonegap.com, we can see that our device is listed.
01:44Now I want to see this debug interface and the simulator at the same time,
01:48so I'll make this window smaller.
01:50Now when I select the Elements panel, I can highlight elements in the simulator,
01:57drill down, and change content in the same way that I changed it before.
02:09Since debug.phonegap.com runs over the Internet, I don't need to have a weinre
02:13server running on my machine that my device can connect to.
02:17One thing to be aware of, since this is going through the Internet, is that you
02:20may see a delay when you try to change properties or inspect the phone.
02:24But in general, this is a quick and easy way to start using weinre from any
02:28device in any computer.
02:31We also have weinre support included within PhoneGap build.
02:37If you go to build.phonegap.com, sign in, and select your application, in the
02:43Settings menu you'll have the option to enable debugging.
02:48When you enable debugging you don't have to manually put the script file that
02:51points to the weinre server in your project.
02:54PhoneGap Build will automatically do that for you.
02:57Save that setting and it will prompt a rebuilding, so all the versions of your
03:01application will now have debugging enabled and when you install them on your
03:05device, you can use the debugging portal to go and remotely inspect the content.
03:11To debug my device I click on the debug button here, and that's going to take me to the portal.
03:17Notice I have a unique ID there that's being generated by PhoneGap Build.
03:21Go back to PhoneGap Build and using a phone, you can take a picture of the QR code,
03:27go to the URL, and install the application on your device.
03:30Make sure that if you're using Android, you allow installation of applications
03:34from sources other than a play store.
03:36Once your application is installed and running, tab back over to the weinre
03:40control panel and you'll see your device list.
03:44You can use all the same functionality that you can use with weinre, but now it's
03:47running remotely on your device, so you can test and debug on the device itself.
03:52debug.phonegap.com provides a hosted solution for debugging applications using weinre.
03:57Applications using PhoneGap Build can be configured to use this cloud service
04:01and somewhat automate the process of getting the application set up for weinre.
Collapse this transcript
Conclusion
Where to go from here
00:00PhoneGap provides an amazing opportunity for web developers to create
00:04mobile applications.
00:05It blurs the line between web technologies and device features.
00:09There are a number of great resources available for you to use to take the next
00:13steps with PhoneGap.
00:14The first is phonegap.com, which is where you find all the up-to-date information
00:19about what's happening with PhoneGap.
00:20Second, I encourage you to look at the docs, at docs.phonegap.com.
00:27This will have all the information about how to use the APIs, as well as some
00:31great getting-started guides for all the platforms.
00:36Next is the PhoneGap blog.
00:38Here you'll find out about community events, potentially where the PhoneGap
00:41developers will be at conferences, and also news about release announcements.
00:47Make sure to follow the PhoneGap team on Twitter.
00:50If you're looking for help with a technical question, the PhoneGap Google Group
00:53is a great way to get a response.
00:57Finally, I encourage you to check out the Apache Cordova project, which is the
01:01open-source project behind PhoneGap. cordova.apache.org has all the information
01:06you need about the project.
01:08It also includes a link on how to contribute,
01:10so you can get involved in the PhoneGap project from the start.
01:13I look forward to seeing what you build, and thank you very much.
Collapse this transcript


Suggested courses to watch next:

jQuery Mobile Essential Training (3h 11m)
Joe Marini


JavaScript Essential Training (5h 31m)
Simon Allardice


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,069 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,024 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked