Custom Cordova Plugins
Cordova Plugin provides a simple way to write native code to your program. On this page, we will describe how to create and add custom Cordova plugins to your Monaca project.
You need to subscribe to a valid plan in order to use custom Cordova plugins. Please refer to Monaca Subscription Plans.
Your custom Cordova plugin needs to follow these requirements:
It should be compatible with Cordova 4.2 or higher.
It must have a
plugin.xml
file and is installable withplugman
command line tool.
It must support iOS and Android platforms.
You can find many Cordova plugins in the internet, but it is likely that they are either obsolete or their versions do not match. Therefore, how to create a custom Cordova plugin from scratch is covered in this page.
Structure of a Cordova plugin
A typical structure of a Cordova plugin is as follows (directories are displayed in bold letters):
plugin_name
src
ios
plugin_name.h
plugin_name.h
www
plugin_name.js
plugin.xml
Before getting started, please download the following zip file containing sample plugin code.
Download a Sample Cordova Plugin
When you extract the package, you will see the following files and folders:
plugin.xml file
plugin.xml
file is a plugin specification file containing plugin's definitions. For more details, please refer to Cordova Plugin Specification.
Below is a plugin.xml
file from the downloaded sample plugin.
hello_world.js file
hello_world.js
is defined as js-module
in plugin spec. Therefore, it will be automatically loaded by cordova.js
, which is included in loader.js
.
For more details about how to write Cordova callback function, please refer to the Plugin Development Guide.
iOS and Android native code
Native code must be written in either Objective-C or Java, depending on the platform. It must have the same function as defined in the JavaScript file. Also, please be aware that all callbacks are asynchronous.
Here are the guides about native code development.
Import a custom Cordova plugin
To import a custom Cordova plugin, please do as follows:
From the Monaca Cloud IDE, go to Configure → Cordova plugin Settings.
The Cordova Plugins page will open. Click the Import Cordova Plugin button.
3. Then, browse your plugin file (zip file) and click OK.
Monaca Debugger with custom Cordova plugin
The standard Monaca Debugger, which can be found in the store such as App Store or Google Play, includes the standard (core) and several third-party Cordova plugins (refer to Third-party Cordova Plugins for the list of all third-party Cordova plugins pre-included in Monaca). For this reason, the standard Monaca Debugger cannot be used with Monaca projects containing custom or other external third-party Cordova plugins. In this case, custom built Monaca Debugger is required to run such projects properly.
Custom built Monaca Debugger is a debugger which is built within Monaca Cloud IDE inside a project containing custom or external third-party Cordova plugins. Please refer to the following links on how to build custom built debugger and the differences between the standard and custom built debugger:
Custom Monaca Debugger for Android
Please make sure to import the custom or external third-party Cordova plugins before building the custom built debugger.
We also offer technical support (fee required) for developing your custom Cordova Plugin. Please contact our support team here.
Testing a custom Cordova plugin
Create a project in the Monaca Cloud IDE using the
Minimum Project Template
.Import a sample custom Cordova plugin you downloaded earlier in Structure of a Cordova plugin.
Add the following snippet in the
index.html
file. The code below shows just a simple message dialog using the sample custom Cordova plugin:Now, run the project in the standard Monaca Debugger. You will see that the message dialog will not be shown because the custom Cordova plugin doesn't exist in the standard debugger.
Build a custom built Monaca Debugger. Please refer to the following links on how to build a custom built Monaca Debugger:
Install the custom built Monaca Debugger.
Open the debugger and check the debugger's information by clicking the About this debugger button.
You should be able to find the custom Cordova plugin as shown in the screenshot below:
Login to the debugger and run the project containing the custom Cordova plugin. The message dialog should show if the project runs properly.
Last updated