Alex
Alex

Reputation: 12089

no device ready and no console.log with xcode using cordova 1.5

This is all the code I have and I do not get neither the logs in xcode nor the deviceReady event (which I don't get on any other platform either. On Ubuntu+Android+Eclipse I do get the console logs, but no deviceReady. nor in chrome )

The js/cordova-1.5.0.js exists and being loaded as indicates an alert statement i've put in there. Any clues where should I look ? Thanks in advance ;)

<div id="d"></div>
<script>
    function foo() {console.log('test'); document.getElementById('d').innerHTML += 'called';}
    window.setTimeout(foo, 5000);
    window.setTimeout(foo, 15000);
    window.setTimeout(foo, 25000);
    window.setTimeout(foo, 35000);
    alert('hi');
    console.log('non timed console.log');

</script>
<script src="js/cordova-1.5.0.js"></script>
<script>    
    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {
        alert('deviceReady');
        //somewhy this never happens
    }

</script>

Screenshot form xcode

Upvotes: 3

Views: 8128

Answers (3)

goosefx
goosefx

Reputation: 51

I know the question was asked 9 month before but I stumbled over the same problem.

If you want debug messages to appear in the weinre console you have to call:

window.cordova.logger.useConsole(false);

after deviceready.

Update:

It seems that you need luck to get console messages into weinre - thats bad :(

Upvotes: 0

cobberboy
cobberboy

Reputation: 6235

As Alex pointed out, console.log is not available until after your PhoneGap device is ready. By calling it too soon, you're triggering a reference error.

Remove ALL of your existing javascript, and try this instead (replacing the alert on the second-last line with your own custom code):

var app = {
    // denotes whether we are within a mobile device (otherwise we're in a browser)
    iAmPhoneGap: false,
    // how long should we wait for PhoneGap to say the device is ready.
    howPatientAreWe: 10000,
    // id of the 'too_impatient' timeout
    timeoutID: null,
    // id of the 'impatience_remaining' interval reporting.
    impatienceProgressIntervalID: null,

    // 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);
        // after 10 seconds, if we still think we're NOT phonegap, give up.
        app.timeoutID = window.setTimeout(function(appReference) {
            if (!app.iAmPhoneGap) // jeepers, this has taken too long.
                // manually trigger (fudge) the receivedEvent() method.   
                appReference.receivedEvent('too_impatient');
        }, howPatientAreWe, this);
        // keep us updated on the console about how much longer to wait.
        app.impatienceProgressIntervalID = window.setInterval(function areWeThereYet() {
                if (typeof areWeThereYet.howLongLeft == "undefined") { 
                    areWeThereYet.howLongLeft = app.howPatientAreWe; // create a static variable
                } 
                areWeThereYet.howLongLeft -= 1000; // not so much longer to wait.

                console.log("areWeThereYet: Will give PhoneGap another " + areWeThereYet.howLongLeft + "ms");
            }, 1000);
    },
    // 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.iAmPhoneGap = true; // We have a device.
        app.receivedEvent('deviceready');

        // clear the 'too_impatient' timeout .
        window.clearTimeout(app.timeoutID); 
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        // clear the "areWeThereYet" reporting.
        window.clearInterval(app.impatienceProgressIntervalID);
        console.log('Received Event: ' + id);
        myCustomJS(app.iAmPhoneGap); // run my application.
    }
};

app.initialize();

function myCustomJS(trueIfIAmPhoneGap) {
    // put your custom javascript here.
    alert("I am "+ (trueIfIAmPhoneGap?"PhoneGap":"a Browser"));
}

Upvotes: 0

Alex
Alex

Reputation: 12089

  1. Console.log works only after deviceReady event

  2. Phonegap uses different phonegap.js files for android and ios, and only the android one is distributed with the downloadable archive. Read Dhaval's comment to learn where to get the ios version.

  3. I used Weinre for debugging and almost missed that it overrides the console.log method, therefore console.log doesn't work with weinre

Upvotes: 4

Related Questions