Twitter: raymondcamden


Address: Lafayette, LA, USA

Somewhat lame iOS Cordova debugging tip

02-17-2014 7,336 views Mobile 14 Comments

A while ago I wrote about iOS Remote Debugging and PhoneGap/Cordova apps. Long story short - it kicks butt and is a great way to debug. (And you can do the same with Android.) There is one small issue though that you may run into and I've got a workaround.

In order to use remote debugging with iOS, you have to run Safari, open the Developer menu, and click on your device or the simulator link.

So far so good - except for one thing. Every time you kill and restart the app, Safari will close the debug window. This is not only annoying, but it also means that any console messages, or errors, that show up before you re-open the window are lost. Android does not have this problem. So what to do if you need to debug the application immediately?

Use an alert.

Yeah, that is really lame, but it also works. Consider the following block.

document.addEventListener("deviceready", init, false);
function init() {
  alert('Lame sauce!');
	console.log('running FS tests');
	window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, errorHandler);
}

function errorHandler(e) {
  var msg = '';

  switch (e.code) {
    case FileError.QUOTA_EXCEEDED_ERR:
      msg = 'QUOTA_EXCEEDED_ERR';
      break;
    case FileError.NOT_FOUND_ERR:
      msg = 'NOT_FOUND_ERR';
      break;
    case FileError.SECURITY_ERR:
      msg = 'SECURITY_ERR';
      break;
    case FileError.INVALID_MODIFICATION_ERR:
      msg = 'INVALID_MODIFICATION_ERR';
      break;
    case FileError.INVALID_STATE_ERR:
      msg = 'INVALID_STATE_ERR';
      break;
    default:
      msg = 'Unknown Error';
      break;
  }

  console.log('Error: ' + msg);
}

function onFSSuccess(fs) {
	console.dir(fs);

    var dirReader = fs.root.createReader();

    dirReader.readEntries(gotFiles,errorHandler);	
}

function gotFiles(entries) {
	console.log("gotFiles success, size of entries is "+entries.length);
	for(var i=0,len=entries.length; i<len; i++) {
		//entry objects include: isFile, isDirectory, name, fullPath
		var s = "";
		s+= entries[i].fullPath;
		if (entries[i].isFile) {
			s += " [F]";
		}
		else {
			s += " [D]";
		}
		console.log(s);     
	}
}

I placed an alert inside the init function that I've tied to the deviceready event. On load, the alert will stop everything, let me open the debugger in Safari, and carry on. I could have put it on line one as well, but in my case I needed to debug when the deviceready event fired, not before.

14 Comments

  • Ronald Klip #
    Commented on 02-17-2014 at 4:06 PM
    Thanks, nice one. usually press reload in the debugger window. Either with the reload button in the DOM inspector, or with Cmd-R / Ctrl-R. It reloads index.html and thus the entire app. And you get the early messages.
  • Commented on 02-17-2014 at 4:36 PM
    Nice - didn't know about that one!

    What would be really cool is if the debugger; command worked like it does in Chrome/desktop.
  • Nadav Greenberg #
    Commented on 02-17-2014 at 4:40 PM
    In ordernot to change the code (sometimes changing it is not an option) I use:
    1. open app
    2. open safari dev tools
    3. set up breakpoints (e.g. in deviceready callback)
    4. in dev tools console do: window.location.reload()
  • Commented on 02-17-2014 at 4:45 PM
    Isn't that the same as the first suggestion - just done via code instead of key combo? But yeah - good idea too. :)
  • Commented on 02-18-2014 at 5:34 AM
    weird as hell, but TREMENDOUSLY useful :)
  • Todd Hensley #
    Commented on 02-20-2014 at 1:43 PM
    I can't get this to work on a physical phone. Safari pages show up, but not PhoneGap apps. It it supposed to work on an actual device, or only the simulator? I'm running Safari 7.0.1 on the Mac and iOS 7.0.4 on an iPhone 5.
  • Commented on 02-20-2014 at 1:47 PM
    Yes, it is supposed to - but did you enable it on the phone? You were supposed to. (I talk about it on the article I link to towards the beginning of the article.)
  • Todd Hensley #
    Commented on 02-20-2014 at 2:05 PM
    Yes, I believe I have the phone and Safari on the Mac set up properly, because I can see Safari pages. PhoneGap apps are not showing up.
  • Commented on 02-20-2014 at 2:33 PM
    I tested with a 3.4 project (brand new), and it works.
  • Dan Baker #
    Commented on 02-28-2014 at 1:33 PM
    Ive had some success using weinre, its a javascript debugging tool which you inject into the page.
  • Commented on 02-28-2014 at 1:41 PM
    Weinre is pretty darn good, yep.
  • Commented on 03-24-2014 at 6:55 AM
    Hey guys.

    If you hit cmd+R while focused on the safari inspector, the app will reload on the device/simulator. This is what I use and it works for 99% of cases (except for first install processes).

    Thanks

    Simon
  • Brad Wedell #
    Commented on 05-20-2014 at 8:19 PM
    If you're using a mac, you can use this gist to create a .app file in automator - gist.github.com/bwdolphin/e7de2d0a21914708bd9a . Then, in terminal (assuming you launch with command line), create a bash alias to call the cordova build command and then launch that app (which has a 1.5 second delay to give the app time to launch on the device before trying to open the console). To polish it off, you can do what the author suggests and add a timeout to your init script (longer than the 1.5 seconds you already wait for the app to load), and voila, you have an app that launches the debug console everytime you build!
  • Commented on 05-20-2014 at 8:29 PM
    Ok, that's kinda slick! Thanks for sharing it.

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty