Search This Blog

Saturday 30 January 2016

Create Custom Cordova plugins using plugman

creating custom plugin is sometimes needed when working on hybrid apps.
creating a custom plugin manually is not so easy as it needs a
1. XML
2. JS interface

If we plugman, its very easy as plugman creates most of your XML and JS interface.
follow the steps below to create one:

1. Install Plugman
2. Create plugin with Plugman
3. Add a package.json to plugin
4. Add platforms
5. Edit your method name in JS and XML files
6. Install using plugman to check if everything is correct.

1. Install Plugman
using CLI it is quite easy to install any plugin.
use the following command to install plugman.

npm install -g plugman

2. Create plugin with Plugman

plugman create --name <pluginName> --plugin_id <pluginID> --plugin_version <version> [--path <directory>] [--variable NAME=VALUE]
ex:plugman create --name Super --plugin_id "org.super.cool" --plugin_version 0.0.0 --variable description="Awesome PLugin"

this will create a plugin.xml and src/, www/ folders required for your plugin.
3. Add a Package.json
Creates a package.json file in the plugin based on values from plugin.xml.
 plugman createpackagejson <directory>

4. Add platforms
plugman platform add --platform_name=ios
This will add the <platform> tags to the plugin.xml as well as create an ios/ source folder in src/

5. Edit your method name in JS and XML files
the above command creates a js entry with name coolMethod in js and xml files
edit them according to your needs.

change the method name and make sure that the name matches the name in your src class.

6. Install plugin to a project to verify
plugman install --platform ios --project /Users/praveenseela/Desktop/Team/Praveen/custom\ plugin/testprj/testprj/platforms/ios/ --plugin /Users/praveenseela/Desktop/Team/Praveen/custom\ plugin/SaveBase64 

install plugin specific to a platform of your cordova project

if everything is correct it will not throw any errors.

in your project js file, you should be call be able to call your method using:
cordova.plugins. SaveBase64.yourMethodName

this way using plugman the burden of creating xml and js interface is simplified.
References:

1 comment: