I am building a PhoneGap App. Unfortunately, when deploying to iOS devices and simulators the deviceready event never fires. I\'m using Phonegap 2.2.0.
To add to olore's answer I ended up using the approach that the code in the default project (that gets built from the ./create script) uses (which is different to the code in the Event docs).
The major differences are (I do not really know which one of these actually are to be taken into account):
cordova-2.2.0.js is located in the root folders are included right before the closing -tag and not in the document's head
deviceready-handling works like:
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
myApp.start(); //this is where I put the call to my App's functionality relying on device APIs
},
// Update DOM on a Received Event
receivedEvent: function(id) { // I didn't really use this, yet I left it in here as it's in the demo
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
The last tag just calls app.initialize()
This seems to work quite fine on iOS and Android and is a little more understandable to me than the double handler nesting from the docs.