mobiletest mobiletest - 2 years ago 1937
Javascript Question

Xamarin iOS communication between Javascript and c#

We implementing an app where we have communication between Javascript and c#. Where, UIWebView has a button to invoke some native functionality. On a UIWebView i have an handler on ShouldStartLoad.

webView.ShouldStartLoad = myHandler;
bool myHandler (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navType)

This gets called everytime page loads. Indeed, i would like to only call it from an event from WebView such as on a button click.
From Javascript i have

window.location.href = "myapp://action?par1=abc&par2=def";

How to call a particular function from custom url?

Calling JavaScript from c#

I am trying to call back JavaScript from c# but it is not calling TestShow() function

wkWebView.EvaluateJavaScript(string.Format("TestShow()"), (r, e) =>
Console.WriteLine("In EvaluateJavaScript");
if (e != null) Console.WriteLine(e);

JavaScript side i have a Alert but it is not showing that alert

function TestShow()
alert("Hello! I am an alert box!!");

Answer Source

You can either continue using UIWebView and parse the NSUrlRequest to see if it is the call you're looking for. Then return true/false accordingly.

A better option would be to use WKWebView and create a custom message handler. Something like this:

1) Implement IWKScriptMessageHandler (tested on the default UIView created by Xamarin UIViewController)

public class UniversalView : UIView, IWKScriptMessageHandler
    public void DidReceiveScriptMessage(WKUserContentController userContentController, WKScriptMessage message)
        var msg = message.Body.ToString();

2) Create user controller with a handler for "myapp" (this = the IWKScriptMessageHandler)

        var userController = new WKUserContentController();
        userController.AddScriptMessageHandler(this, "myapp");

3) Create a config with the controller

        var config = new WKWebViewConfiguration
            UserContentController = userController

4) Create the WKWebView with the config

var webView = new WKWebView(new CGRect(10, 100, 500, 500), config);

5) Call "myapp" from your JS code

        <html><head><meta charset = "utf-8"/></head><body>
            <button onclick="callCsharp()">Click</button>"
            <script type="text/javascript">
                function callCsharp(){ 



EDIT: In regards to evaluating JS from C# you need to be sure the HTML page has finished loading or otherwise the call will result in an error. You can handle navigation events by implementing IWKNavigationDelegate

public class UniversalView : UIView, IWKScriptMessageHandler, IWKNavigationDelegate

    public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        webView.EvaluateJavaScript("callCsharp()", (result, error) =>
            if (error != null) Console.WriteLine(error);

Assign it to the WKWebView you created:

        var webView = new WKWebView(new CGRect(10, 100, 500, 500), config)
            WeakNavigationDelegate = this
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download