This highly depends on what platforms and version of platforms your are trying to target.
If you really want to use a WebView
, you might check out the XLabs Hybrid WebView which does something similar I believe.
Other than XLabs' methods, you can easily get back a string from an iOS renderer using the UIWebView.EvaluateJavascript()
method and then you can always override WebChromClient.OnJsAlert
in a Xamarin Android WebView
renderer (link), which I have successfully used back to Android 4.1.
Something like (Let me know if something does not work or is missing, I typed this out and removed extra code so there may be minor issues)...
Xamarin Forms Code:
public class SomethingWebView : WebView { }
iOS WebView Renderer with UIWebViewDelegate
:
[assembly: Xamarin.Forms.ExportRenderer(typeof(SomethingWebView), typeof(Your.Droid.Namespace.SomethingWebViewRenderer))]
namespace Your.Droid.Namespace.Renderer {
public class SomethingWebViewRenderer : WebViewRenderer {
protected override void OnElementChanged(VisualElementChangedEventArgs e) {
base.OnElementChanged(e);
if(e.OldElement == null) {
Delegate = new SomethingWebViewDelegate();
}
}
}
internal class SomethingWebViewDelegate : UIWebViewDelegate {
public override void LoadingFinished(UIWebView webView) {
string something = webView.EvaluateJavascript("(function() { return 'something'; })()");
}
}
}
Android WebView Renderer with WebViewClient
and WebChromeClient
:
[assembly: Xamarin.Forms.ExportRenderer(typeof(SomethingWebView), typeof(Your.iOS.Namespace.SomethingWebViewRenderer))]
namespace Your.iOS.Namespace.Renderer {
public class SomethingWebViewRenderer : WebViewRenderer {
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e) {
base.OnElementChanged(e);
if(e.OldElement == null) {
Control.Settings.JavaScriptEnabled = true;
Control.SetWebViewClient(new SomethingWebViewClient());
Control.SetWebChromeClient(new SomethingWebChromeClient());
}
}
}
internal class SomethingWebViewClient : WebViewClient {
public override void OnPageFinished(Android.Webkit.WebView view, string url) {
base.OnPageFinished(view, url);
view.LoadUrl("javascript:{(function() { window.alert('something'); })()};");
}
}
public class SomethingWebChromeClient : WebChromeClient {
public override bool OnJsAlert(Android.Webkit.WebView view, string url, string message, JsResult result) {
if(message.StartsWith("something") { //This is where you would look for your magic string, anything starting with your magic string is what you would extract and/or act on
//Do something....
result.Cancel(); //This cancels the JS alert (there are other talked about methods of doing this but this works for me)
return true; //This tells the WebView "we got this"
}
return base.OnJsAlert(view, url, message, result); //Let the WebView handle this since we did not find out special string
}
}
}
Finally, another method I looked at involved using JavaScript to redirect the page to a new URL, then in the event that is used to tell the WebView whether or not it should go forward with the redirect, you pull out the WebView's current URL and, if you find your magic string, you do something. This would have worked great except for the fact that URLs can only be like 255 characters long so it would not work for my needs.