Search code examples
cordovaphonegap-pluginsphonegap-buildcordova-plugins

How to install/use cordova plugins?


I have installed cordova 5.3.3, I create my apps, for android, with:

cordova create Project
cd Project
cordova platform add android

After that, I zip my file with:

cd ..
zip -r Project.zip Project

And upload it to phonegap build at:

https://build.phonegap.com/apps

After creating there the apk and instaling it on my android the apps seem the work fine, except when I try to use plugins.
The plugin functions always fail while the rest of the javascript code works fine.

I add plugins has follows:

cordova plugin add org.apache.cordova.vibration
cordova platform rm android
cordova platform add android

And if I list my plugins with:

cordova plugin ls

I get:

cordova-plugin-whitelist 1.0.0 "Whitelist"
org.apache.cordova.vibration 0.3.13 "Vibration"

I get the same behavior when using phonegap (version 5.3.6), or when just uploading the zip file with only the "www" folder and the "config.xml" file.
I've also read these similar questions in stackoverflow:

phonegap plugins 5.1.1 cli don't work

Cordova plugins not working

But they don't seem to help me in my case.

The index.html code:

<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <title>Hello World</title>
</head>
<body>
    <div class="app">
        <h1>Teste Vibra</h1>
        <button type="button" id="vibrar">Vibrar</button>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script src="js/jquery-2.1.4.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">

    </script>

</body>

The javascript code:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    $(function(){
        $( "#vibrar" ).click(function() {
            navigator.vibrate(3000);
            alert("abc");
        });
    })
}

I'm using the JQuery library 2.1.4.


Solution

  • Okay. You have a fundamental problem. Somehow you have not read the correct documentation.

    Please read this to help you get started
    Read the bold points, then read the body of each section as it applies.


    Top Mistakes by Developers new to Cordova/Phonegap

    In this case, you have made mistake #1,

    I QUOTE:

    1. Thinking Phonegap is just one system

    There are three similar platforms, Cordova (CLI), Phonegap (CLI), and Phonegag Build and five system (counting Cordova/Phonegap SDK1). People often confuse the three very similar platforms and get tangled in the minor variations that differentiate them.

    Oddly enough, the best explanation comes from something built on top of Angular, and wraps the Cordova/Phonegap CLI. - Ionic.

    From the Ionic Blog The Last Word on Cordova and PhoneGap

    PhoneGap proper was created around 2009 by a startup called Nitobi as an open source way to access the "native" environment through an embedded Web View in a native app. The goal of the project was to make it possible to build the bulk of a mobile app experience with pure web technologies like HTML5, CSS, and Javascript, but still be able to call into native code when necessary.

    In 2011 Adobe purchased Nitobi and with it the rights to the PhoneGap brand, and the open source core was donated to the Apache Software Foundation under the name Cordova.

    Read more on the Ionic Blog