Xamarin iOS Javascript与C#之间的通信 [英] Xamarin iOS communication between Javascript and c#

查看:142
本文介绍了Xamarin iOS Javascript与C#之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在实现一个应用程序,可以在Javascript和c#之间进行通信.我们的UIWebView有一个按钮来调用某些本机功能.在UIWebView上,我在ShouldStartLoad上有一个处理程序.

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

每次页面加载时都会调用此方法.确实,我只想从WebView的事件(例如单击按钮)中调用它.从Java语言开始,我有

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?

从c#调用JavaScript

我正在尝试从c#回调JavaScript,但是没有调用TestShow()函数

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端我有一个警报,但未显示该警报

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

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

推荐答案

您可以继续使用UIWebView并解析NSUrlRequest以查看它是否是您要查找的呼叫.然后相应地返回true/false.

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.

一个更好的选择是使用WKWebView并创建一个自定义消息处理程序.像这样:

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

1)实现IWKScriptMessageHandler(在Xamarin UIViewController创建的默认UIView上进行测试)

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)使用用于"myapp"的处理程序(这= IWKScriptMessageHandler)创建用户控制器

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

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

3)使用控制器创建配置

3) Create a config with the controller

        var config = new WKWebViewConfiguration
        {
            UserContentController = userController
        };

4)使用配置创建WKWebView

4) Create the WKWebView with the config

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

5)从您的JS代码中调用"myapp"

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");

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

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

编辑:关于从C#评估JS,您需要确保HTML页面已完成加载,否则调用将导致错误.您可以通过实现IWKNavigationDelegate

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);
        });
    }

将其分配给您创建的WKWebView:

Assign it to the WKWebView you created:

        var webView = new WKWebView(new CGRect(10, 100, 500, 500), config)
        {
            WeakNavigationDelegate = this
        };

这篇关于Xamarin iOS Javascript与C#之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆