Xamarin iOS communication between Javascript and c# - Stack Overflow

We are implementing an app where we have munication between Javascript and c#. Our UIWebView has a butt

We are implementing an app where we have munication 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!!");
}

We are implementing an app where we have munication 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!!");
}
Share Improve this question edited Dec 15, 2017 at 3:11 nmilcoff 1,0846 silver badges21 bronze badges asked Apr 11, 2016 at 21:21 User382User382 87420 silver badges42 bronze badges 3
  • Have you thought about using the HybridKit plugin github./chkn/HybridKit? – QiMata Commented Apr 11, 2016 at 23:36
  • Not really HybridKit. I tried JSBridge but it has some limitations and not working in our case. So i want to do it without any plugin – User382 Commented Apr 12, 2016 at 0:03
  • Do you know if HybridKit plugin good for the App store submission. – User382 Commented Apr 12, 2016 at 18:17
Add a ment  | 

1 Answer 1

Reset to default 6

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742408447a4438324.html

相关推荐

  • Xamarin iOS communication between Javascript and c# - Stack Overflow

    We are implementing an app where we have munication between Javascript and c#. Our UIWebView has a butt

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信