I have what seems to be an unusual requirement that my colleagues and I haven\'t been able to implement in our Xamarin project. What we are trying to do is dynamically proce
Both these device-dependent techniques use JavaScript evaluation/execution after the HTML page has finished loading in order for to receive the entire html contents as a JavaScript result (edit the JavaScript in the examples if your requirements to not require the entire page to be return).
You can capture the loaded html within a WebView
by assigning a subclassed WebViewClient
that implements IValueCallback
. The OnReceiveValue
method will contain the html content after the page has finished loading:
WebViewClient
Class:public class EmbeddedWebViewClient : WebViewClient, IValueCallback
{
public EmbeddedWebViewClient(WebView view)
{
view.Settings.JavaScriptEnabled = true;
}
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
Log.Info("SO", $"Page loaded from: {url}");
view.EvaluateJavascript("(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();", this);
}
public void OnReceiveValue(Java.Lang.Object value)
{
// "value" holds the html contents of the loaded page
Log.Debug("SO", ((string)value).Substring(0, 40));
}
}
WebView webview = FindViewById<WebView>(Resource.Id.webview);
webview.SetWebViewClient(new EmbeddedWebViewClient(webview));
webview.LoadUrl("https://xamarin.com");
You can capture the loaded html within a WKWebView
by assigning a IWKNavigationDelegate
to it. Either implement it within its own class or the controller that contains the WKWebView
IWKNavigationDelegate
Implementation:public class NavigationDelegate : NSObject, IWKNavigationDelegate
{
[Export("webView:didFinishNavigation:")]
public async void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
var content = await webView.EvaluateJavaScriptAsync("(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();");
var html = FromObject(content);
Console.WriteLine((html.ToString()).Substring(0, 40));
}
}
wkwebview = new WKWebView(UIScreen.MainScreen.Bounds, new WKWebViewConfiguration());
wkwebview.NavigationDelegate = new NavigationDelegate();
Add(wkwebview);
wkwebview.LoadRequest(new NSUrlRequest(new Uri("https://xamarin.com")));
Note: This same basic logic can be done with a UIWebView
, but WKWebView
is so much faster that if you do not need to support iOS 7 clients, I would personally focus on using WKWebView