TechnoMic
TechnoMic

Reputation: 1

Cordova: always deviceready has not fired

I have another problem with Cordova. I want to use the plugins "cordova.custom.plugins.exitapp" and "cordova-plugins-printer" in Cordova 7.1.0.
On the server side, I have build document.AddEventListener into $(document).ready(function() {});

.js:

$(document).ready(function(){

...

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

...

}),


function exitFromApp() {

    console.log("NAVIGATOR: " + navigator);
    console.log("NAVIGATOR.APP: " + navigator.app);
    console.log("NAVIGATOR.APP.EXITAPP: " + navigator.app.exitApp());

    navigator.app.exitApp();
}


Whether I use addEventListener or not, the Android Studio always says:
- deviceready has not fired after 5 seconds.
- Channel not fired: onPluginsReady
- Channel not fired: onCordovaReady

But the difference is that addEventListener does not call the exitFromApp() function.
When I call exitFromApp() directly, it works, but navigator.app is undefined (or cordova.plugins / window.plugins is undefined).
The cordova.js is called in the header.php and is therefore always available.

If I use the plugins via index.html on the tablet, it works.

I have set the permissions in config.xml:

<access origin="*" />
<allow-navigation href="*" />
<allow-intent href="*" />

Installation:
Cordova 7.1.0
cordova-plugin-inappbrowser 2.0.1
cordova-plugin-network-information 2.0.1
cordova-plugin-whitelist 1.3.3
cordova.custom.plugin.exitapp 1.0.0
phonegap.plugin-barcodescanner 7.0.1
cordova-plugin-printer 0.7.3

Upvotes: 0

Views: 806

Answers (1)

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

the deviceready event is fired before $(document).ready() You want to do you stuff after both have been fired.. try this..

var DomReady = new Promise(done=>$(document).ready(done));
var deviceReady = new Promise(done=>document.addEventListener("deviceready", done, false));
Promise.all[DomReady, deviceReady].then(()=>{
  // both device and dom are ready
});

... Or you could just put the deviceReady call outside the documetnt ready call since I'm sure the device ready will always be first.

Upvotes: 1

Related Questions