Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap deviceready not firing using Cordova 2.2.0 in iOS

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.

When I deploy the same code to Android (using the Android-specific cordova.js file of course) the App will work perfectly.

When I replace the deviceready with a jQuery-ready() the app will load on iOS as well, yet it will then lack access to the device specific APIs.

The cordova.js is loaded as I will see a simple alert message that I put inside of it, yet deviceready never fires and the APIs are never exposed.

My HTMLs head:

<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script> <!-- yes it is the iOS version -->
<script src="js/jquery-1.8.2.min.js"></script>
<script src="js/app.js"></script>

My JS:

function doStuff(){
//app functionality
}
document.addEventListener('deviceready', doStuff, false);

But somehow stuff will only get done on Android...

like image 890
m90 Avatar asked Nov 02 '12 13:11

m90


3 Answers

in my html I have a onload that triggers that adding of an event listener to deviceready

      function onDeviceReady() {
        console.log("we are an app");
        MyApp.initialize_phonegap();
      }

      function onBodyLoad() {   
        document.addEventListener("deviceready", onDeviceReady, false);
      }

    </script>

  </head>

  <body onload="onBodyLoad()">
like image 161
olore Avatar answered Nov 01 '22 19:11

olore


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 folder
  • <script>s are included right before the closing </body>-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 <script> 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.

like image 32
m90 Avatar answered Nov 01 '22 18:11

m90


It seems to make a difference if you are adding the deviceready listener before or after the cordova.js:

I was not able to find any documentation on this, but cordova.js intercepts calls to addEventListener + removeEventListener and calls only deviceready callbacks which has been added before cordova.js.

Solution in my case was just to rearrange the script order:

<script>
document.addEventListener('deviceready', ...)
</script>
<script src="cordova.js"></script>
like image 40
mrak Avatar answered Nov 01 '22 19:11

mrak