2

I am working on developing iOS application using Xamarin. I have a requirement to call c# method from JavaScript inside UIWebView. How could we achieve that?

The following is html content is loading into UIWebView

const string html = @"
                    <html>
                      <body onload=""setBarcodeInTextField()"">
                        <p>Demo calling C# from JavaScript</p>
                        <button type=""button"" 
                                onClick=""CSharp.ScanBarcode('txtBarcode', 'setBarcodeInTextField')"">Scan Barcode
                        </button>
                        <input type=""text"" value="""" id=""txtBarcode""/>

                        <script type=""text/javascript"">



                        function setBarcodeInTextField() {

                            alert(""JS"");
                        }

                        </script>

                      </body>
                    </html>"; 

Also, i am getting about://(null) in alert message (onload specified on body tag for displaying alert) when UIWebView loads the html content.

2
  • did you have any luck with this? Commented Apr 10, 2018 at 6:49
  • Refer to here and here Commented Apr 10, 2018 at 7:24

1 Answer 1

4

One solution to trigger C# method from website shown in WebView compontent is to:

1) Initialize a navigation to a website in your web code, for example

http://scancode/providedBarCode

2) Then you can override a webview method which is called before navigation actually happens. You can intercept a call there with parameters and ignore the actual navigation

Xamarin Forms (Navigating method of WebView)

webview.Navigating += (s, e) =>
{
    if (e.Url.StartsWith("http://scancode/"))
    {
        var parameter = e.Url.Split(new[] { "scancode/" }, StringSplitOptions.None)[1];
        // parameter will be providedBarCode

        // perform your logic here

        // cancel the actual navigation
        e.Cancel = true;
    }
};

Xamarin iOS (ShouldStartLoad method of UIWebView)

webViewControl.ShouldStartLoad = (webView, request, navType) =>
{
    var path = request.Url.AbsoluteString;
    if (path.StartsWith("http://scancode/"))
    {
        var parameter = path.Split(new[] { "scancode/" }, StringSplitOptions.None)[1];
        // parameter will be providedBarCode

        // perform your logic here

        // cancel the actual navigation
        return false;
    }

    return true;
}
Sign up to request clarification or add additional context in comments.

6 Comments

HI Michal, Thank you so much for your response for my question.
HI Michal, Thank you so much for your quick response for my question.i am unable to find Navigating event for UIWebView on Xamarin iOS and also i would like to mention is there is button in html.i would like call C# function(CSharp.ScanBarcode('txtBarcode', 'setBarcodeInTextField') onClick event of button.
I edited my answer with Xamarin.iOS code snipper. Put the code which you want to call above "return false", you have access there to your parameter.
@NagendraBabu that's great :) please accept my answer, so others can see that it's a good solution to this kind of problems
i found one more best solution for calling c# method from javascript in UIWebview Xamarin iOS is from developer.xamarin.com/api/type/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.