Here's an answer for a similar question in Objective C but I'm not sure what's the right way to translate it to MonoTouch.
Basically, I want to be able to catch JavaScript errors and know at least filename and line number—unfortunately, window.onerror
doesn't give this crucial information.
In particular, I'm not sure if I should expose a native library or if I can write this in pure MonoTouch.
I adopted Pablo's
UIWebView+Debug
category inspired by Robert Sanders and Kresimir Prcela's answers.Note that his code also includes a private API call you can use to enable remote web inspector.
(For some reason this thing doesn't work for me though.)
Update: here's how to debug UIWebView in Mountain Lion—you need to download an older version of Chromium.
Remember to only use private API while debugging—if you submit an app and forget to remove these calls, Apple will reject your app. For this reason both Xcode and MonoDevelop code use
DEBUG
conditions.Here is the complete source code I'm using:
Xcode project
UIWebView+Debug.h
UIWebView+Debug.m
MonoTouch project
AppDelegate.cs
Because I couldn't get
LinkWith
attribute to work properly, I put this in project properties:iPhone Build Project Options
Simulator
Device
Custom Commands > Before Build
Command
Simulator
Device
Working directory
Finally, these are the build scripts:
compile-i386
compile-arm
I realize this may not be the best way to do this, but it works just fine.
Feel free to post a simpler solution if you know one.