How to see the javascript errors of PhoneGap app in Xcode?

JavascriptIosXcodeDebuggingCordova

Javascript Problem Overview


I want to debug my PhoneGap app in Xcode, but its Console can not show javascript errors.

Javascript Solutions


Solution 1 - Javascript

The most elegant way to view and debug JavaScript errors in your Cordova/PhoneGap App is by attaching the Web Inspector from your Safari browser to the Web View in your iOS App (but, like Tom Clarkson already mentioned, you will need at least iOS 6).

  • On your iPad or iPhone use the Settings App to enable Web Inspector in the Advanced Settings for Safari
  • Connect your device to a Mac via USB (it will then appear under the Develop menu of Safari)
  • Start your App
  • Navigate to the Web View you want to debug
  • On the Mac, from the Safari Develop menu, select the name of your device and the App (its HTML-page), from its sub menu
  • A Web Inspector window will open, enabling you to browse the DOM, set breakpoints etc.

screen dump of Safari Develop menu on OS X

Apples documentation on setting this up

A thorough third party tutorial

Alternatively you could connect Chrome’s Web Inspector to iOS devices after installing iOS WebKit Debug Proxy. This also opens up the ability to do the inspection from Linux or Windows.

Remote access to your iOS’s HTML, CSS and JavaScript has gotten even more flexible nowadays because you can install the RemoteDebug iOS WebKit Adapter on top of aforementioned Debug Proxy. Because this adapter translates the WebKit Remote Debugging Protocol to the Chrome Debugging Protocol, these (on all their supported platforms) become available as alternative debugging and inspection tools:

  • Visual Studio Code
  • Chrome DevTools
  • Mozilla Debugger

BTW, remote debugging with the Safari Web Inspector works even in combination with the iOS Simulator.


Minimum version of Desktop Safari per iOS version

For each version of iOS you will need a specific minimum version of Desktop Safari in order to use remote web inspection, see the list below.

iOS 6
Safari 6+
iOS 7
Safari 6.1+
iOS 8
Safari 7.1+
iOS 9
Safari 8+
iOS 10
Safari 9+/10+? Please comment; always try https://developer.apple.com/safari/technology-preview/">Safari Technology Preview
iOS 11
Safari 11+
iOS 12
Safari 12+

Solution 2 - Javascript

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.

Solution 3 - Javascript

Note that with 0.9.2 (released today), console.log has been standardized across the platforms for logging (with debug.log deprecated).

There is a function that is available on the desktop WebView that is not exposed in the iOS UIWebView that will catch all errors (I'm trying to hack that functionality into a plugin, which uses private APIs, but the plugin would only be for development), but for now do what Kris suggested above and put try catch blocks on code and use console.log

To quickly catch possible syntax errors, when developing I have the page loaded in desktop Safari and quickly refresh it with the webkit error console viewable.

Solution 4 - Javascript

debug.log will send messages to the XCode console in Phonegap (allowing you to either log the result of an exception or do some debugging), however, you are correct that you have to debug other javascript errors in Safari (either on the desktop or on the iphone with Debug Console turned on). I have yet to find a Javascript error, that was caused by running on the iphone and wasn't present when debugging with the console turned on in Safari (though I know there are a few differences between the WebView and Safari on the iphone).

Solution 5 - Javascript

For making javascript debugging work in Xcode I would take a look at the following.

http://phonegap.com/2011/05/18/debugging-phonegap-javascript/<br /> http://www.daveoncode.com/2010/01/12/debugging-phonegap-applications-using-xcode-console/<br />

As as far as additional troubleshooting goes...
To start with you could run the app in safari on you pc and utilize safari's debugger (or chrome as both are running similar rendering engines). This won't hit on the advanced logic errors and many of your api issues but it at the very least should help with troubleshooting many issues (basic javascript, HTML5 etc....).

Solution 6 - Javascript

I just came across Weinre

It's a remote javascript debugger for phonegap. You can either setup your own Weinre server, or use the one at http://debug.phonegap.com/

It seems to work well - very impressed so far.

Solution 7 - Javascript

If you use iOS 6, you can simply attach the safari web inspector (on the develop menu of desktop safari) to your app and get full javascript debugging.

There are a couple of areas where it is a bit limited - startup errors and plugin calls - but it works well for pretty much anything else.

Solution 8 - Javascript

To view all errors in javascript console, I agree to use this event listener

<script type="text/javascript">
    window.onerror = function(err,fn,ln) {alert("ERROR:" + err + ", " + fn + ":" + ln );};
    var errorVar = objectDoesntExists.properyDoesntExist;//this will simulate an error
</script>

However, unless you have the cordova plugin installed, it wont show on XCodes "console". Go to your project folder and type this:

? cordova plugin add cordova-plugin-console

This will allow the javascript command 'console.log('some string') to show on XCode.

Note you will need git, etc... but if you are editing your phonegap project in xcode, you will most probably have it!

PS Make sure you put the cordova.js script plug-in before any use of console.log

<script type="text/javascript" src="/cordova.js"></script>

Solution 9 - Javascript

Put this in the beginning of your index.html

<script type="text/javascript">
    window.onerror = function(err,fn,ln) {alert("ERROR:" + err + ", " + fn + ":" + ln);};
    var errorVar = objectDoesntExists.properyDoesntExist;//this will simulate an error
</script>

Solution 10 - Javascript

Here's a simple way that worked for me:

  • cd to the directory containing your index.html file in the terminal

  • Start a http server using python by invoking (I used python 2.7):

    python -m SimpleHTTPServer

  • View the page in safari by entering the address of the HTTPServer in a browser, for me the URL was:

      http://0.0.0.0:8000/
    
  • Open developer tools:

In chrome this is alt+command+i. View the console tab, may need to refresh the page.

In Safari: Safari --> Preferences --> Advanced --> check "Show Develop Menu". Develop menu --> Show error console (or alt+command+c). Refresh the page. Hitting CTRL+5 opens the issues tab.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmockeeView Question on Stackoverflow
Solution 1 - JavascriptᴠɪɴᴄᴇɴᴛView Answer on Stackoverflow
Solution 2 - JavascriptelMarquisView Answer on Stackoverflow
Solution 3 - JavascriptShazronView Answer on Stackoverflow
Solution 4 - JavascriptKris EricksonView Answer on Stackoverflow
Solution 5 - JavascriptTerranceView Answer on Stackoverflow
Solution 6 - Javascriptasgeo1View Answer on Stackoverflow
Solution 7 - JavascriptTom ClarksonView Answer on Stackoverflow
Solution 8 - JavascriptVinnie AmirView Answer on Stackoverflow
Solution 9 - JavascriptToolkitView Answer on Stackoverflow
Solution 10 - Javascriptstorm_m2138View Answer on Stackoverflow