Cordova - Device


Advertisements

This plugin is used for getting information about the user’s device.

Step 1 - Installing Device Plugin

To install this plugin, we need to run the following snippet in the command prompt.

C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-device

Step 2 - Adding Button

We will be using this plugin the same way we used the other Cordova plugins. Let us add a button in the index.html file. This button will be used for getting information about the device.

<button id = "cordovaDevice">CORDOVA DEVICE</button>

Step 3 - Adding Event Listener

Cordova plugins are available after the deviceready event so we will place the event listener inside the onDeviceReady function in index.js.

document.getElementById("cordovaDevice").addEventListener("click", cordovaDevice);	

Step 4 - Creating Function

The following function will show how to use all possibilities the plugin provides. We will place it in index.js.

function cordovaDevice() {
   alert("Cordova version: " + device.cordova + "\n" +
      "Device model: " + device.model + "\n" +
      "Device platform: " + device.platform + "\n" +
      "Device UUID: " + device.uuid + "\n" +
      "Device version: " + device.version);
}

When we click the CORDOVA DEVICE button, the alert will display the Cordova version, device model, platform, UUID and device version.

Cordova Device Alert
Advertisements