16.11.2015 Views

PG2

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

28 Chapter 2 PhoneGap Development, Testing, and Debugging<br />

API availability, a web developer building PhoneGap applications must instruct<br />

the container to notify the web application when the PhoneGap APIs are available.<br />

Any application processing that requires the use of the APIs should be executed<br />

by the application only after it has received its notification that the APIs are<br />

available.<br />

In the HelloWorld2 application, this notification is accomplished through the<br />

addition of an onload event defined in the page’s body section, as shown here:<br />

<br />

Within the onBodyLoad function, the code registers an event listener that instructs<br />

the application to call the onDeviceReady function when the device is ready, when<br />

the PhoneGap application container has finished its initialization routines:<br />

document.addEventListener("deviceready", onDeviceReady, false);<br />

In this example application, the onDeviceReady function simply displays a<br />

PhoneGap alert dialog (which is different from a JavaScript alert dialog), letting<br />

the user know everything is OK:<br />

navigator.notification.alert("PhoneGap is ready!")<br />

In production applications, this function could update the user interface (UI) with<br />

content created through API calls or do whatever other processing is required by<br />

the application. You’ll see an example of this in the next sample application.<br />

The PhoneGap Navigator<br />

Many of the APIs implemented by PhoneGap are instantiated from the Navigator object.<br />

Unfortunately, it’s not consistent; some do and some do not. Be sure to check the API<br />

documentation before calling an API.<br />

Leveraging PhoneGap APIs<br />

Now that we know how to configure an application to wait until the PhoneGap<br />

APIs are available, let’s build an application that actually uses the PhoneGap APIs<br />

as shown in the following HelloWorld3 application.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!