Skip to content

Instantly share code, notes, and snippets.

@Corstiaan84
Last active January 23, 2016 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Corstiaan84/56a0346d67c9077b45aa to your computer and use it in GitHub Desktop.
Save Corstiaan84/56a0346d67c9077b45aa to your computer and use it in GitHub Desktop.
public partial class DomLoadedExampleController : UIViewController
{
private UIWebView _webView;
private event EventHandler DomReady;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
//initialze UIWebView instance
_webView = new UIWebView (new Rectangle (0, 0, (int)UIScreen.MainScreen.Bounds.Width, (int)UIScreen.MainScreen.Bounds.Height));
//wire up DomReady event handler
DomReady += (object sender, EventArgs e) => {
//handle the event
new UIAlertView ("DOM Loaded!", "The DOM has loaded!", null, "OK", null).Show ();
};
//this is called when a link/a element is clicked inside the webview and the location.href is set to a new value
//we can catch it to determine if the request.Url contains our "domReady" string
_webView.ShouldStartLoad = (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navType) => {
if(request.Url.AbsoluteString.Contains("domReady")) {
//url contains "domReady". This is our cue :-)
if(DomReady != null) {
//fire event
DomReady(this, EventArgs.Empty);
//do not allow UIWebView to follow this url
return false;
}
}
return true;
};
_webView.LoadHtmlString ("the html shown above", NSBundle.MainBundle.BundleUrl);
Add (_webView);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment