Giter Site home page Giter Site logo

rootsoft / create-a-custom-cordova-plugin Goto Github PK

View Code? Open in Web Editor NEW
31.0 1.0 10.0 6 KB

How to create a custom cordova plugin and bridge it between your native code and a new or existing Cordova project

License: Apache License 2.0

cordova cordova-plugin cordova-android plugin

create-a-custom-cordova-plugin's Introduction

Custom Cordova Plugin


This guide shows you how to create a custom cordova plugin and bridge it between your native android code and your new or existing Cordova project.

First of all make sure you got everything installed and the latest version that you need to create your own cordova project. (npm, Cordova CLI, etc)

Cordova


Create a new cordova project

If you already have an existing cordova project, you can skip this step. Create a new cordova project using CLI, from scratch or using your desired IDE e.g. (WebStorm new project -> PhoneGap/Cordova App).

$ cordova create LogicLinkApp com.endare.logiclinkplugin

Add the platform to your cordova project

Add the platforms that you want to target your app. We will add the 'android' platform and ensure they get saved to config.xml and package.json:

$ cordova platform add android

Plugman


Install Plugman

Install Plugman globally using the following command:

$ npm install -g plugman

You can use the plugman help command to display the help file and documentation about plugman.

Create a plugin

Navigate to the folder where you want to create your plugin.

$ plugman create --name <pluginName> --plugin_id <pluginID> --plugin_version <version> [--path <directory>] [--variable NAME=VALUE]

This will create a folder in your specified path with the plugin name you defined.

Example:

$ plugman create --name LogicLinkPlugin --plugin_id cordova-plugin-logiclinkplugin --plugin_version 0.0.1

Add the platform to your plugin

This generates a generic template for your plugin for your desired platform.

$ plugman platform add --platform_name android

Add a Package.JSON file

Creates a package.json file in the plugin based on values from plugin.xml.

 $ plugman createpackagejson <directory>

You can use . for your current directory.

Here you will need to enter some information about your plugin.

Install the plugin

Now we install the plugin in our existing Cordova project for our platform. The code below is specific for adding a plugin to an Cordova-Android project.

$ plugman install --platform android --project platforms/android --plugin ../LogicLinkPlugin/

Note! This will copy your existing plugin to the src folder of your desired platform. Do not edit plugin files in your Cordova Project! Whenever you make changes to your plugin, you have to uninstall the plugin using:

$ plugman uninstall --platform android --project platforms/android --plugin ../LogicLinkPlugin/

and reinstall it with the plugman install command above.

Usage


The JavaScript interface provides the front-facing interface, making it perhaps the most important part of the plugin. You can structure your plugin's JavaScript however you like, but you need to call cordova.exec to communicate with the native platform, using the following syntax:

Here is how each parameter works:

  • function(winParam) {}: A success callback function. Assuming your exec call completes successfully, this function executes along with any parameters you pass to it.

  • function(error) {}: An error callback function. If the operation does not complete successfully, this function executes with an optional error parameter.

  • service: The service name to call on the native side. This corresponds to a native class, for which more information is available in the native guides listed below.

  • action: The action name to call on the native side. This generally corresponds to the native class method. See the native guides listed below.

  • [/ arguments /]: An array of arguments to pass into the native environment.

Go to the plugin.xml file in your Plugin project and look for the value of the <clobbers target="value">. Copy this value. This will be used in the Cordova Application to bridge between the Cordova and Android application

In your <pluginname>.js file, you can write your own methods by extending the exports function with a new exec.

exports.coolMethod = function(arg0, success, error) {
    exec(success, error, "LogicLinkPlugin", "coolMethod", [arg0]);
};

Add the following code to wherever you need to call the plugin functionality:

cordova.plugins.<PluginName>.<method>();

For example:

function testPlugin() {
    cordova.plugins.LogicLinkPlugin.coolMethod("coolMethod", function(response){
        console.log(response);
    }, function(error){
        console.log(error);
    });
}

create-a-custom-cordova-plugin's People

Contributors

rootsoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

create-a-custom-cordova-plugin's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.