By default a PhoneGap app consists only of a WebView showing the HTML code written by you. The native functionalities offered by the phone (Batter, Camera, Contacts etc.) is available through special JavaScript calls, but only if you install the proper plugins into your project.
Plugins can be separated in 3 categories:
In order to check which plugins are installed in your project you need to run a shell/cmd and go to the root folder of the project than type in the command:
phonegap plugins ls
This command will list all the installed plugins.
For installing plugins the command is
phonegap plugins add package-name
for plugins in the npm registry or phonegap example :
phonegap plugins add org.apache.cordova.camera" to install the official camera plugin
or
phonegap plugins add url-to-github-projects
example:
phonegap plugins add https://github.com/apache/cordova-plugin-console.git
There is also the possibility to search for plugins directly from console with
phonegap plugins search multiple-search-words
example:
phonegap plugins search bar code
For more tutorials on the PhoneGap check this page but use “phonegap” instead of “cordova”.
If you are going to build only using the PhoneGap Build cloud service there is no need to install the plugins locally since you are not going to upload them on the server. All you need to do is edit the config.xml to tell the builder which plugins to include. The most simple way to do this is by adding the line
<gap:plugin name="package name" />
example:
<gap:plugin name="com.phonegap.plugins.example" />
you can also add version to it to be sure that in future build you won't get another version than the one you tested:
<gap:plugin name="com.phonegap.plugins.example" version="2.2.1" />
For more information check here
For a list of 3rd party plugins in the repository check here
While some features need plugins to become available to our code, others are just mapped over the native HTML5 javascript methods and events. Example: window.DeviceOrientationEvent returns information from the gyroscope
Such is also the case of the 3 HTML5 storage APIs which do not need installing plugins.
localStorage is a simple system to store key/pair values. In order to store object is recommended to stringify them.
Supported platforms:
Set:
window.localStorage.setItem("key", "value");
Get:
var value = window.localStorage.getItem("key");
Remove:
window.localStorage.removeItem("key");
Supported platforms:
WebSQL is a client side SQL-query driven database.
In order to create or open an existing database:
var db = window.openDatabase(database_name,version,description, allocated_memory);
If a database with a previous existing name and version is opened the already existing db will be accessed, otherwise a new one will be created. In order to run SQL queries they must be grouped into transactions:
db.transaction(function (tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)'); tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")'); tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")'); });
In order to retrieve the results we will need to give a callback to the execute sql method:
tx.executeSQL(sql_query,optiona_arguments,callback);
where the callback should be a function that takes transaction and result_set as argument:
tx.executeSQL(sql_query,optiona_arguments,function(tx,result){ //the insert id of the last insert is in result.insertID //the rows returned by the query are in result.rows // result.rowsAffected contains the number of rows. });
Supported platforms:
Since neither WebSQL nor IndexedDB don't offer full support yet we won't focus on those to technologies in this class. For a detailed tutorial on IndexedDB check MDN (Mozilla Developer Network)