- Shopping Bag ( 0 items )
Create cross-platform mobile apps without having to learn a new language!
Create cross-platform mobile apps without having to learn a new language!
Within Beginning PhoneGap, each new chapter builds on the lessons taught in the previous so that by the end of the book, you can have mobile apps up and running!
"NOTE: Several significant changes to the PhoneGap software occurred shortly after this book was published. The author has written extra pages to address these and other changes. You can download the updates to this book from the book's support page on the Wrox website."
WHAT YOU WILL LEARN IN THIS CHAPTER:
* Using PhoneGap to build an application * Exploring the history of PhoneGap * Exploring what you can build with PhoneGap * Understanding the basics of a PhoneGap application
Welcome to PhoneGap! You're reading this book because you've probably heard about PhoneGap, and want to learn more about how you can use it to build cross-platform mobile applications. This book delves into different parts of the PhoneGap API, and teaches you how to use the PhoneGap tools to build different applications.
Before getting into the nitty-gritty details, though, it's time to get a 10,000-foot overview of PhoneGap and what it can do. In this chapter, you learn about the basics of using PhoneGap, as well as a bit of the history of PhoneGap. By the end of this chapter, you will be familiar with the basics of building a PhoneGap application.
As you can see in Figure 1-1, according to PhoneGap's website (www.phonegap.com), PhoneGap is an "HTML5 app platform that allows you to author native applications with web technologies."
Looking at a Quick Showcase
You might be thinking to yourself that PhoneGap sounds promising, but you're not quite sure what it can actually do. Without getting into a lot of technical detail quite yet, the best way to illustrate its capabilities is to offer a brief showcase of successful projects.
As shown in Figure 1-2, "Diary Mobile" is a mobile app that helps you to better organize your life. It features a Journal, a Task Manager, and a Planner. You can keep notes on your Android or iPhone, and then synchronize your information back to a centralized web repository. The app works offline, and automatically saves items online once you're back online.
As shown in Figure 1-3, "NFB Films" is an Android-only PhoneGap app that lets you watch more than 2,000 films for free. Users can search for content, browse films by channel or subject, and share their favorite documentaries, animated shorts, and trailers with other users.
"Just One More"
As shown in Figure 1-5, "Orbium" is a fast-paced puzzle game available on iPhone, Android, and webOS. It features high-quality graphics and an intuitive touch-screen interface.
Taking a Basic Run-Through
So, what's involved, generally speaking, when you create a PhoneGap application? This entire book is devoted to drilling down into this topic with great detail, but first, it might be helpful to understand the overall process involved in working with PhoneGap.
Here is a typical workflow for building your first PhoneGap application:
Obviously, there's a lot more to it than that, especially in what you can and can't do on specific devices. Before getting into all of that, however, let's take a quick look at the history of PhoneGap.
HISTORY OF PHONEGAP
PhoneGap solves two problems for developers:
Getting to Know the Origins of PhoneGap
The first PhoneGap code was authored at the San Francisco iPhoneDevCamp in August 2008. A contributing driver for its creation was a simple fact that almost every single newbie iPhone developer runs into: Objective-C can be a very unfamiliar environment for web developers, and there are lots more web developers out there than there are Objective-C ones.
Within a year, PhoneGap was winning awards and was starting to support the Android platform, making it even more useful to the growing family of mobile developers who need to support code on more than one platform.
Assessing PhoneGap's Current Status
Currently, PhoneGap is at Version 1.x (1.0.0.rc2 was released July, 2011). PhoneGap offers support for the following features across many of the major smartphone devices:
* Notifications (alerts, sounds, and vibrations)
If you're developing for iPhone or Android devices, chances are that all of that functionality is supported. If you're developing for Blackberry, webOS, Windows Phone 7, Symbian, or Bada devices, there may be a few things missing. (For example, there is no support for the camera, compass, or storage features on Windows Phone 7. The older Blackberry models support geolocation, notifications, and network reachability, but that's about it.)
A road map of future releases includes updates to the Contact API, bringing it up-to-date with the latest World Wide Web Consortium (W3C) specifications. Furthermore, plans are in the works for the following features (with this just being a taste, not the complete road map):
* Web notifications
* HTML media capture
* Calendar API
* Internationalization support
* Command-line builds
* Plug-in architecture (will help third-party developers extend PhoneGap)
* Network loss/regain events
Understanding What PhoneGap Is Good/Bad At
Another thing PhoneGap is good at is bridging the gap (hence the name) between your standard web technologies, and the unique capabilities inherent in your smartphone. By using the PhoneGap API components, you can quickly and easily access the onboard camera, pull up the contacts, or work with the compass.
If you need to connect your application with a remote web service (typically, a web service or a RESTful API), you can easily bring in tools like jQuery to create powerful Ajax handlers. Of course, you're also free to hand-roll your own XmlHttpRequests.
Given all this, a word of caution is in order. Just because you code an application using PhoneGap and it works on one device (iPhone, for example) doesn't automatically mean that it will work on others. You may have to test and tweak for Android, Blackberry, or Windows Phone 7 devices. So, don't imagine for a second that this is a "code once, deploy multiple times" scenario.
If you are working with multiple devices, you will need separate environments for each wrapper. For example, you won't be able to maintain your Android PhoneGap wrapper with Xcode. Another way of framing this is that you are potentially simplifying how much your web code must change to run on different devices, but you still must maintain separate wrappers.
If you create a PhoneGap application that relies heavily on animation and graphics, you might tax the device you are on — this is true whether you are working with PhoneGap or native code. Also, the same is true if you are creating huge dependencies with a remote API — your application should work well in offline mode, because you will never know if the person using the app has lost connectivity.
UNDERSTANDING THE BASICS OF A PHONEGAP APPLICATION
Now it's time to dig a little deeper into PhoneGap. Before jumping into an overview of the API specifics, it might be useful to go over some examples of what you can do with the major components — just to get your creative juices flowing for possible application ideas.
What You Can Build with PhoneGap
Earlier, you saw a quick showcase of different PhoneGap applications. Let's take a brief look at the possibilities with some of the major components of the API.
Working with Contacts
The contacts list is a fairly ubiquitous feature, available on most smartphones. With PhoneGap, you can easily do the following with the contacts feature:
* Create a contact using the create() method.
* Save a contact using the save() method.
* Find a contact using the find() method.
* Clone a contact using the clone() method.
* Remove a contact using the remove() method.
The PhoneGap API supports various property fields for a contact (such as a display name, a nickname, phone numbers, e-mails, addresses, birthday, gender, photos, time zone, and so on), most of which you can run through contacts.find() to get a list of matching users.
The clone() method allows you to quickly copy a contact that's in memory, and then change just the properties that are different. For example, you might have a group of people who all share the same physical address. Cloning the original contact would allow you to work through the list of property changes very quickly.
The remove() method works as expected — use it to remove a contact from the device's contacts database.
From this basic list of methods and functions, you could easily incorporate various views that allow users to search through their contacts, create new contacts, make changes to existing contacts, or even delete contacts. For example, your application might help users make batch operations on all of their contacts based on search criteria (such as a ZIP code or existence of a phone number).
Working with the Camera
Most smartphones have built-in cameras. The PhoneGap API provides two ways to capture images, and one is giving access to the camera via the camera object. The second is by using the Media Capture API (which you will learn a little about later in this chapter) Specifically, the camera.getPicture() method takes a photo using the camera, or retrieves a photo from the device's photo album, depending on what source type you have passed in (either CAMERA or PHOTOLIBRARY).
You can also choose to have the camera provide you with a Base64-encoded photo image (this is the default setting), or the image file location. Once you have this information, you can use it to render an image, post the data to a remote server, or save the data locally.
An interesting option is to capture an editable photo. This introduces all kinds of possibilities with respect to being able to crop an image (for example) after the photo has been captured by the device.
What are some of the applications you can build using the Camera API? A good one might be a photo-sharing application that allows you to take a picture, perform some basic edits, and then publish that photo to a remote web server.
Other application possibilities include a note-taking tool that lets a user take a photo of something, and then add notes to that image. Or, you could even have an application that lets shoppers take a picture of items they find in stores, and add them to a wish list they can then share with friends.
Working with Geolocation
Another hallmark of modern smartphones is their geolocation capabilities. Most smartphones will be able to use GPS or some other technology to tell you what latitude and longitude you're currently at with some degree of precision. Of course, if the device is using cell phone towers to triangulate your position, your location won't be accurate at all. And, if you have no network connectivity, you're completely out of luck.
The PhoneGap Geolocation API lets you get a device's current position (in longitude and latitude, but other details like altitude might also be included) and to watch a position in case it changes. This would be very useful if you were trying to track the device's movement.
You have many different ways to approach geolocation in your applications. You can use geolocation data to enrich other data — for example, you could add a latitude and longitude to any pictures taken by your application. Or, you could add geolocation data to any notes created by the user. Or, you could send geolocation data to a remote web server so that dispatchers can keep tabs on field workers.
Working with Media Files
In PhoneGap, the Media Capture API isn't simply a good means for capturing photos. You can also use it to capture audio and video data as well. As expected, the Media Capture API allows you to start and stop recording, play, pause, and stop media files, and even display an audio file's duration.
From an application point of view, you can use the Media Capture API to create different voice- and video-recording apps — think of how useful it would be to have a tool that lets you record audio and video, and add written notes or photos in one package (for those of who use Evernote, you'll recognize that this describes it perfectly). You could also build an app that lets you sample different audio files. This could be fairly useful if you're building a mobile application that showcases a catalog of music or spoken-word recordings — PhoneGap will play locally saved files, as well as those on a remote web server.
Excerpted from Beginning PhoneGap by Thomas Myer Copyright © 2012 by John Wiley & Sons, Ltd. Excerpted by permission of John Wiley & Sons. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.
CHAPTER 1: INTRODUCING PHONEGAP 1
CHAPTER 2: INSTALLING AND CONFIGURING PHONEGAP 17
CHAPTER 3: BASIC WALKTHROUGH 31
CHAPTER 4: EVENTS 45
CHAPTER 5: WORKING WITH THE DEVICE, THE NETWORK, ANDNOTIFICATIONS 59
CHAPTER 6: ACCELEROMETER 73
CHAPTER 7: COMPASS 85
CHAPTER 8: GEOLOCATION 99
CHAPTER 9: MEDIA 113
CHAPTER 10: CAMERA 129
CHAPTER 11: STORAGE 143
CHAPTER 12: FILES 157
CHAPTER 13: CONTACTS 179
CHAPTER 14: CAPTURE 189
CHAPTER 15: CREATING A NOTE-TAKING APPLICATION 197
APPENDIX A: ANSWERS TO EXERCISES 213
APPENDIX B: TOOLS FOR PHONEGAP 235
APPENDIX C: PHONEGAP.JS 247
APPENDIX D: PHONEGAP PLUG-INS 333