Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

console.log not working in an iOS PhoneGap 3.0 app

Tags:

xcode

ios

cordova

I am upgrading an app from PhoneGap 1.9 to PhoneGap 3.0. The console.log() function is not working anymore. Previously, the ouput was written in the XCode console. What's the best way to restore the logging feature?

I have read: PhoneGap 2.0 doesn't show console.log in XCode but in PhoneGap 3.0 console.log does not work even after the deviceReady event.

I am also interested to see the javascript errors directly in xcode.

like image 898
poiuytrez Avatar asked Oct 04 '13 14:10

poiuytrez


4 Answers

You need the debug console plugin added to your project:

phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-console.git

In later versions of phonegap/cordova, to add the debug console plugin in your project:

cordova plugin add org.apache.cordova.console

like image 78
Sam T Avatar answered Nov 01 '22 13:11

Sam T


It turned out, at least in my case, that deviceready function for the logger was not getting called. The final line in logger.js.

document.addEventListener("deviceready", logger.__onDeviceReady, false);

The solution (or really a work-around) is to call the logger.__onDeviceReady function from your deviceready listener function:

function onDeviceReady() {
    if (window.cordova.logger) {
        window.cordova.logger.__onDeviceReady();
    }
}

document.addEventListener('deviceready', onDeviceReady, false);
like image 39
Jonathan Dixon Avatar answered Nov 01 '22 15:11

Jonathan Dixon


I've found JSconsole.com extremely useful for remotely capturing console logs from mobile devices.

Heres how you set it up:

  1. In you apps index.html, Include(change the ID):

      <script src="http://jsconsole.com/remote.js?<MAKE UP SOME UNIQUE ID>"></script>
    
  2. On your computer, Go to jsconsole.com and type :listen <YOUR UNIQUE ID>

Open your app on your mobile device, you will see console logs on your computer.

like image 9
Fostah Avatar answered Nov 01 '22 14:11

Fostah


Add the console plugin as in the Sam's answer and ensure to include cordova.js on every page otherwise no plugin works.

When using phonegap plugins I was adding phonegap.js in script tag then I noticed that plugins (any) works on index.html page only. When I changed phonegap.js to cordova.js, plugins(notification, camera etc) started working on rest of the pages. If this helps anyone.

like image 6
Muhammad Qasim Avatar answered Nov 01 '22 15:11

Muhammad Qasim