Cordova plugins

A Cordova plugin is an open source, cross-platform mobile development architecture that allows the creation of multiplatform-deployable mobile apps. These apps can access native component features of devices using an API having web technologies such as HTML 5, JavaScript, and CSS 3. Apache Cordova Plugins are integrated into IBM Worklight Android and iOS projects. In this chapter, we will describe how Apache Cordova leverages the ability to merge the JavaScript interface as a wrapper on the web side in a native container with the device native interface on the mobile device platform.

The most critical aspect of Cordova plugins is to deal with the native functionalities such as camera, bar code scanning, contacts list, and many other native features, currently running on multiple platforms. JavaScript doesn't provide such extensibility to enhance the scripting with respect to the native devices.

In order to have a native feature's accessibility, we provide a library corresponding to the device's native feature so that JavaScript can communicate through it. When the need arises for a web page to execute the native feature functionality, the following points of access are available:

  • The scenario has to be implemented in platform-specific manner, for example, in Android, iOS, or any other device
  • In order to handle requests and responses between web pages and native pages, we need to communicate to/from web and native pages that are encrypted

By selecting the first option from the preceding list, we would find ourselves implementing and developing platform-dependent mobile applications. As we are in need of implementing mobile applications for a cross-platform mobile, and because it leads to provide cost-ineffective solutions, it is not a wise choice for Enterprise Mobile Development Solutions. It seems to be a really poor extensible for future enhancements and needs.

In this scenario, a developer needs to declare a custom Cordova plugin with a functionality that is not yet available in it. After creating a wrapping layer, this functionality will be used from the JavaScript code. As it is a standard-based architectural framework, Apache Cordova arrives with some extensible proficient architectures for these plugins that simplifies integration and communication with the native device code and its features. Some key plugins provided by Cordova are as follows:

  • Accelerometer: This is a motion sensor that detects the change in movement relative to the current device's orientation
  • Camera: This plugin takes a photo using the camera or can be used to retrieve a photo from the device's album
  • Geolocation: This plugin allows you to detect the location information of the device in the form of latitude and longitude

We can implement or reconstruct any custom plugin if there is a need for a functionality that is not available within the prebuilt plugins. The Apache Cordova plugin consists of two different parts for Android; they are as follows:

  • Java code that executes native features within the Android OS
  • A JavaScript wrapper (an interface for executing Java code)
  • In Java code, we need to extend the org.apache.cordova.api.Plugin class
  • The plugin could be useable if we can override the execute method provided in the org.apache.cordova.api.Plugin class
  • All plugins should be registered in the res/xml/plugins.xml file

Meanwhile, having an assurance for the secure requests for other external domain, every domain should be whitelisted in the file named res/xml/cordova.xml.

The following is the configuration code for the Cordova plugin in Java:

<cordova>

<access origin="https://developer.com" />

<!– allow secure requests to developer.com –>

</cordova>

A code wrapper for the Cordova plugin in JavaScript has the following structure:

CordovaCustomPlugin.prototype.executeNativeFunction = function(SuccessCallback, FailCallback, param){..}
  • param: This callback function is passed to the plugin from the web page calling Web Page
  • SuccessCallback: This is a callback function for a successful call
  • FailCallback: This is a callback function for an unsuccessful call

After adding a code wrapper, the plugin is added to a windows.plugin object:

cordova.addConstructor(function() {
cordova.addPlugin("cordovaCustomPlugin",new CordovaCustomPlugin());});

Besides this, in JavaScript, you can use windows.plugin, which can be called by the following code:

window.plugins.cordovaCustomPlugin.executeNativeFunction (SuccessCallback, FailCallback, $("#Div").val());
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.171.125