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:
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:
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:
org.apache.cordova.api.Plugin
classexecute
method provided in the org.apache.cordova.api.Plugin
classres/xml/plugins.xml
fileMeanwhile, 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 PageSuccessCallback
: This is a callback function for a successful callFailCallback
: This is a callback function for an unsuccessful callAfter 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());
18.189.171.125