Getting started with PhoneGap
Creating Mobile applications is something a lot of web developers are keen to get into.
The problem with creating these applications when coming from a web background is that new languages need to be learned.
What is PhoneGap ?
PhoneGap is an open source framework that aids in the development of cross-platform applications with languages familiar to web developers.
For anyone from a web background this is a great solution and it also has the benefit of being a cross-platform approach to mobile development.
Since we all want to work with different platforms, there’s no installation guide that’s better than PhoneGap’s own guide, follow the instructions there to install PhoneGap in your preferred environment.
The Device API’s
The good thing about working with PhoneGap is that we have access to a lot of device API’s like:
- Accelerometer — The accelerometer allows us to tap in to the motion sensor in the mobile device.
- Camera — We can capture a photo using the device’s camera.
- Compass — With compass we can get the direction the device is pointing in.
- Capture — The capture provides us access to the audio, image, and video capture capabilities of the device.
- Connection — Check if the user has an internet connection and get its details.
- Contacts — With this API we can access the contacts database on the device.
- Device — This API helps us get information about the device itself.
- Events — This API allows us too hook up to a user event and add a callback to it. An example could be the device ready event, lowering down the sound or even hooking up a function when the user’s battery is critically low.
- Geolocation — This API is widely used in the mobile world and it makes your application aware of where it’s located.
- Globalization — The globalization object obtains information and executes specific functions taking into consideration the user’s locale and timezone.
- Media — With the Media API you can record and play audio files.
- Notification — This API gives you a way to notify the user in a visual, audible or tactile way.
- InAppBrowser — The InAppBrowser is used to launch URL’s in a web browser.
- Splashscreen — Enables developers to show or hide the application’s splash screen.
- Storage — With this API you can hook into the device storage options.
If for instance, you want your app to measure the device’s motion you would use the accelerometer API. A simple example measuring three axis would look something like this:
document.addEventListener("deviceready", loaded, false);
'Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n'
alert("There was an error");
As you can see the first thing we did was check if the device was ready, if that comes back true we start the accelerometer and get its current position and in there we passed a success callback and an error callback; in the former I created an alert with the acceleration in all 3 axis and in the latter just a simple error alert to let the user know that something went wrong.
In this first part we took a look at what PhoneGap is, what its capabilities are, and we even saw how to use one of its device API’s. We’ll continue with this in the second part of this tutorial where we’ll be looking at more device APIs like the camera and geolocation.