first of all, I want to thank miguel.de.icaza for this answer! Helped us a lot! But - here's the big BUT, here's our problem. In the UIWebView we start a "little" javascript App, created with SenchaTouch, that displays some charts. To give it a little look and feel, there's some interaction with the graphs. And sometime, we need to get some more data from the backend (e.g. for different chart types) using an AJAX request. But since the backend is saved with an self signed certificate, which doesn't work with the webView, as we know.
So, what we try to accieve is an AJAX request from our JS-app to the MonoTouch-App code, so that the Mono-app gets the data for the webview and passes it to a Javascript function, that updates the charts with the new data.
But as of right now, we're not able to get this to work, because as soon as we call the location.href = "app://get/chartData" we're logging a "about:blank" request, right before the "app://get/chartData" request, that - of course - blanks the webview and the current chart is gone. Using an simple AJAX request doesn't work for any reason. Meaning, the AJAX request doesn't call the ShouldLoadStart callback. Maybe, someone knows why?
Here's our MonoTouch code so far. This was just for testing the general workflow!! ;-)
NSUrl fileUrl = NSUrl.FromFilename("index.html");
NSUrlRequest req = new NSUrlRequest(fileUrl);
this.KPIWebView.LoadRequest(req);
this.KPIWebView.ShouldStartLoad = myHandler;
this.KPIWebView.LoadFinished += delegate {
indicator.StopAnimating ();
this.KPIWebView.Hidden = false;
};
bool myHandler(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navType)
{
Console.WriteLine(request.Url.ToString());
if(request.Url.ToString() == "about:blank")
{return false;} // doesn't help to avoid loading the "about:blank"
if(request.Url.ToString() == "ttl://data/salesGrowth/global")
{
string strData = Connection.Login("http://[yourIP/path]/salesGrowth/global");
return false;
try{
this.KPIWebView.EvaluateJavascript("Util.loadStoreData("+strData+")");
return false;
}
catch(Exception ex)
{ Console.WriteLine(ex.ToString()); }
return true;
} }
Any comments, ideas and help is greatly appreciated.
Thanks!
Edit: After some more googleling I found out, that the thing with the about:blank request is a known issue with Senach Touch http://www.sencha.com/forum/showthread.php?123284-What-s-up-with-the-about-blank-page. Finally, we were able getting it to run by using our own url schema:
location.href = "app://action?jsCallback=MonoCallback&jsErrorCallback=ErrorCallback¶mName1=name1¶mVal1=value1&..."
In the webview.ShouldStartLoad
callback, we're using the System.Uri namespace to check for the app
protocol and to get all the parameters and javascript callback function names. Now we can do our request from within the mono-app. After all the action, we can call the jsCallback function with webview.EvaluateJavascript(jsCallback+"("+strValues+")");
So, everything works fine now, even asynchronous!
Hope some of you, struggeling the same workflow issue with a self signed certificate within the webview, find this description helpful. If you have more questions about our "workaround" feel free to contact me.
Thanks!