Xamarin iOS communication between Javascript and c#

时光毁灭记忆、已成空白 提交于 2020-02-29 04:19:05

问题


We are implementing an app where we have communication between Javascript and c#. Our 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!!");
}

回答1:


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();
        System.Diagnostics.Debug.WriteLine(msg);
    }
}

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(){ 

window.webkit.messageHandlers.myapp.postMessage("action?par1=abc&par2=def");

                 }</script></body></html>";

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
{

    [Export("webView:didFinishNavigation:")]
    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
        };


来源:https://stackoverflow.com/questions/36559483/xamarin-ios-communication-between-javascript-and-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!