I am trying to use the ZeroConf plugin for Cordova. I never used any Cordova plugin before.
These are the steps I did :
cordova create myApp
cordova platform add android
cordova platform add ios
cordova platform add browser
cordova plugins add
Afterwards, I changed the default onDeviceReady
function so that "index.js" file (in the myApp/www/
directory) looks as follow :
var app = {
...
onDeviceReady: function() {
app.receivedEvent('deviceready');
if(cordova && cordova.plugins) {
console.log('cordova.plugins is available');
} else {
console.log('cordova.plugins NOT available');
}
},
...
};
app.initialize();
Unfortunately it always logs "cordova.plugins NOT available". I first checked if cordova
exists and it does, however, cordova.plugins
is undefined. Note that i'm testing this in the browser (cordova run browser
) to find the problem, once cordova.plugins
won't be undefined anymore I will be able to use the ZeroConf plugin.
I searched for hours, read a lot of similar topics but none could help for me.
What I tried :
cordova plugin ls
--> ZeroConf plugin is present.cambiocreative.cordova.plugin.zeroconf 1.0.9 "Cordova ZeroConf Plugin"
Verified that
index.html
loads thecordova.js
scriptI edited the
config.xml
file (inmyApp/www/config.xml
) and added<plugin name=".cambiocreative.cordova.plugin.zeroconf" version="1" />
, but that did not helped. Also tried<feature name="ZeroConf"><param name="android-package" value=".cambiocreative.cordova.plugin.ZeroConf" /><param name="onload" value="true" /></feature>
without success.
This is the order in which i include the scripts in index.html
(in myApp/www/index.html
) :
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
EDIT : I tried accessing the ZeroConf
object directly from the "zeroconf.js" file, without success.
I edited onDeviceReady
function :
onDeviceReady: function() {
app.receivedEvent('deviceready');
var el = document.getElementById("zcOK");
if(typeof ZeroConf !== 'undefined') {
el.innerHTML = "ZEROCONF READY";
} else {
el.innerHTML = "ZEROCONF NOT READY";
}
}
But this displays "ZEROCONF NOT READY"...
This is how my index now look like (i only added a paragraph with id="zcOK"
to display the above).
<p id="zcOK">ZeroConf test</p>
<!-- ORDER IN WHICH THE SCRIPTS ARE LOADED -->
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="../plugins/.cambiocreative.cordova.plugin.zeroconf/www/zeroconf.js"></script>
<script type="text/javascript" src="js/index.js"></script> <!-- Contains the onDeviceReady function -->
However, now I don't know if "ZeroConf" is undefined because there is some problem with the plugin or because it would not be accessible from within "index.js" because it was included in "index.html" ?
I am trying to use the ZeroConf plugin for Cordova. I never used any Cordova plugin before.
These are the steps I did :
cordova create myApp
cordova platform add android
cordova platform add ios
cordova platform add browser
cordova plugins add https://github./cambiocreative/cordova-plugin-zeroconf
Afterwards, I changed the default onDeviceReady
function so that "index.js" file (in the myApp/www/
directory) looks as follow :
var app = {
...
onDeviceReady: function() {
app.receivedEvent('deviceready');
if(cordova && cordova.plugins) {
console.log('cordova.plugins is available');
} else {
console.log('cordova.plugins NOT available');
}
},
...
};
app.initialize();
Unfortunately it always logs "cordova.plugins NOT available". I first checked if cordova
exists and it does, however, cordova.plugins
is undefined. Note that i'm testing this in the browser (cordova run browser
) to find the problem, once cordova.plugins
won't be undefined anymore I will be able to use the ZeroConf plugin.
I searched for hours, read a lot of similar topics but none could help for me.
What I tried :
cordova plugin ls
--> ZeroConf plugin is present.cambiocreative.cordova.plugin.zeroconf 1.0.9 "Cordova ZeroConf Plugin"
Verified that
index.html
loads thecordova.js
scriptI edited the
config.xml
file (inmyApp/www/config.xml
) and added<plugin name=".cambiocreative.cordova.plugin.zeroconf" version="1" />
, but that did not helped. Also tried<feature name="ZeroConf"><param name="android-package" value=".cambiocreative.cordova.plugin.ZeroConf" /><param name="onload" value="true" /></feature>
without success.
This is the order in which i include the scripts in index.html
(in myApp/www/index.html
) :
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
EDIT : I tried accessing the ZeroConf
object directly from the "zeroconf.js" file, without success.
I edited onDeviceReady
function :
onDeviceReady: function() {
app.receivedEvent('deviceready');
var el = document.getElementById("zcOK");
if(typeof ZeroConf !== 'undefined') {
el.innerHTML = "ZEROCONF READY";
} else {
el.innerHTML = "ZEROCONF NOT READY";
}
}
But this displays "ZEROCONF NOT READY"...
This is how my index now look like (i only added a paragraph with id="zcOK"
to display the above).
<p id="zcOK">ZeroConf test</p>
<!-- ORDER IN WHICH THE SCRIPTS ARE LOADED -->
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="../plugins/.cambiocreative.cordova.plugin.zeroconf/www/zeroconf.js"></script>
<script type="text/javascript" src="js/index.js"></script> <!-- Contains the onDeviceReady function -->
However, now I don't know if "ZeroConf" is undefined because there is some problem with the plugin or because it would not be accessible from within "index.js" because it was included in "index.html" ?
Share Improve this question edited Feb 15, 2016 at 19:05 KeV asked Feb 15, 2016 at 15:13 KeVKeV 2,8913 gold badges22 silver badges31 bronze badges 2- cordova plugins do not work in the browser; you have to test them on device or in the device simulator. – George Stocker Commented Feb 15, 2016 at 16:33
-
@GeorgeStocker Thanks, I was supposing something like that. However, I did the test in an iPhone simulator too (by running the xcode project made by cordova and displaying the value of
cordova.plugins
but there too it isundefined
). – KeV Commented Feb 15, 2016 at 16:37
3 Answers
Reset to default 7Examining the config.xml and the JS file of cordova-plugin-zeroconf I found this declaration:
<js-module src="www/zeroconf.js" name="ZeroConf">
<clobbers target="cambiocreative.CDVZeroConfig" />
</js-module>
So the plugin API should be available at this name space: cambiocreative.CDVZeroConfig
In fact as of Cordova Plugins documentation:
... tags are allowed within
<js-module>
:
<clobbers target="some.value"/>
indicates that the module.exports is inserted into the window object aswindow.some.value
. You can have as many<clobbers>
as you like. Any object not available on window is created. ...
Probably the plugins that you are including dont use the cordova.plugin window. Just try to call the plugin functions, and check if they work.
For those who faced (cordova.plugins === undefined)
, but it's 100% should be.
/src/plugins/plugin_name/plugin.xml
Find tag like this:
<js-module src="www/plugin_name.js" name="Plugin_name">
<clobbers target="cordova.plugins.plugin_name" />
</js-module>
/scr/config.xml
If the tag doesn't exist just copy-paste it before </widget>