Question

I can't get the WebIntent plugin to load on my Phonegap project.

On adb I get the error:

D/CordovaLog(27881): file:///android_asset/www/js/script.js: Line 28 : Uncaught TypeError: Cannot read property 'webintent' of undefined

Phonegap version 3.4.0-0.19.13

I am using this plugin that I found on Phonegap Build website: https://github.com/Tunts/WebIntent

The steps I took:

On terminal, ran phonegap plugin add https://github.com/Tunts/WebIntent

Confirmed that the plugin was installed with phonegap plugin list

In my script.js, I added some sample code from the project:

var address = "some place";

window.plugins.webintent.startActivity({
    action: window.plugins.webintent.ACTION_VIEW,
    url: 'geo:0,0?q=' + address},
    function() {},
    function() {alert('Failed to open URL via Android Intent')}
);

The error is from the above line, window.plugins.webintent.startActivity({

The plugin is loaded at res/xml/config.xml, automatically gets added this line:

<feature name="WebIntent">
    <param name="android-package" value="net.tunts.webintent.WebIntent" />
</feature>
Was it helpful?

Solution

Plugin functions can only be called after the event deviceready has triggered. Any calls made to plugins before that will most likely fail. To prevent this, you can format the loading of you app like the following example:

HTML:

<body onload="onLoad()">

JS:

function onLoad() {
    document.addEventListener('deviceready',onDeviceReady,false);
}

function onDeviceReady() {
    //Code to render your app's main view
    //any inits or plugin setups
    //hide splash screen if using the splash screen plugin
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top