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 process the contents of any page that a browser navigates to rather than simply the content returned from the initial URL it accesses.
I have seen solutions for accessing the content returned this way:
Xamarin: How to get HTML from page in WebView?
However this code will only run when the browser receives the first html response from the initial URL it is pointed to. We need to dynamically process any content the browser receives even if it is redirected to a different URL or if the user clicks on links in the content. We have implemented this in our .NET native Windows clients using the WebBrowser.DocumentCompleted event, which is raised whenever the control loads content. We have not found any event raised or method called when the Xamarin WebViewRendered loads content except on the first load, so we have not been able to get the content of subsequent resources.
To summarize: how can I dynamically get the content of any URL that a web browser navigates to? The answer can include anything accessible in a .NET Xamarin project deployable to Android and iOS including third party open source code.
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).
Xamarin.Android
You can capture the loaded html within a
WebView
by assigning a subclassedWebViewClient
that implementsIValueCallback
. TheOnReceiveValue
method will contain the html content after the page has finished loading:Example
WebViewClient
Class:Example WebViewClient usage:
Xamarin.iOS
You can capture the loaded html within a
WKWebView
by assigning aIWKNavigationDelegate
to it. Either implement it within its own class or the controller that contains theWKWebView
Example
IWKNavigationDelegate
Implementation:Example WKWebView usage:
Note: This same basic logic can be done with a
UIWebView
, butWKWebView
is so much faster that if you do not need to support iOS 7 clients, I would personally focus on usingWKWebView