简体   繁体   中英

Phonegap - Javascript debugging in Xcode

I am working on a phonegap based project. I'd like to use some debug tools, to be able to debug some variables etc into XCode console, etc. Now, I've found, that in order to do this, I need to call function console.log.

The problem is, however, when running the application in simulator no debug info is displayed in XCode console... I am using phonegap version 0.9.3, what am I doing wrong?

Thanks for an answer

Paste the following somewhere near the start of your document so that it gets executed before any of your other JavaScript.

<script type="text/javascript">
    window.onerror = function(message, url, lineNumber) {
        console.log("Error: "+message+" in "+url+" at line "+lineNumber);
    }
</script>

And enjoy viewing details of your Javascript errors in the Xcode console window.

UPDATE: The above technique will log errors such as undefined variables. But syntax errors such as missing commas will still cause the entire script to break without logging anything.

Therefore you should add the following to the start of your onDeviceReady function:

console.log('Javascript OK');

If you don't see "JavaScript OK" appearing in your log window when the app launches, then it means you have a syntax error somewhere.

To save hunting for missing commas, the easiest thing is to paste your code into a Javascript validator such as this one:

http://www.javascriptlint.com/online_lint.php

and let it find the error for you.

Hopefully that takes some of the pain out of debugging.

This feature was completed very recently (two days ago), and is not included in the 0.9.3 release. You can see this ticket in the issue tracker for more information.

To get this working, you have to pull the latest code from the PhoneGap GitHub repository . From a shell:

$ git clone git://github.com/phonegap/phonegap-iphone.git

... and then go about building up the source from scratch. You should see it then!

Alternatively , you replace your console.log statements with debug.log , and that will work in 0.9.3.

Lukas, there is a fantastic tool you can use to debug your phonegap project.

With iWebInspector you basically have Firebug for your Phonegap Application. You can even live-change the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM