MonoTouch: Handle Events from UIWebView


Handle taps on buttons and links within a UIWebView.


I really like how Readability, Pinterest and other apps hide or altogether don’t use a visible UINavigationBar on appropriate views.

image image

Instead, the view is all about the content. I suspect many are using a UIWebView for the content.

The question then becomes, how does the user navigate the view stack from within a UIWebView.

Readability has a left to right swipe gesture that pulls he page away. I suspect this may be one UIViewController that shows a UIWebView over the top of the other content instead of pushing another view onto the stack. If you take a moment in Readability to pull the article away to partially reveal the list, you can scroll the list and see the article. I could be wrong…of anyone knows different do tell. I’m sure there’s more than one way to do this.


In the meantime, I want to add a back image link at the top of my HTML content, and handle that action from the view code.



UIWebView provides ShouldStartLoad where we can see what the component is about to load. At that point we can intercept the request and take a different action.

The Code:

In the HTML I have a link to the anchor #back like this:



To load images into the UIWebView from the local resource bundle, I set the base URL of the request to NSBundle.MainBundle.BundleUrl.

When the user touches that link the webViewShouldStartLoad handles it and pops the view controller as desired.

Categories: iPhoneMonoTouchXamarinPermalink