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.

Vega
  • 27,856
  • 27
  • 95
  • 103
  • did you have any luck with this? – Michał Żołnieruk Apr 10 '18 at 06:49
  • Refer to [here](https://stackoverflow.com/questions/9826792/how-to-invoke-objective-c-method-from-javascript-and-send-back-data-to-javascrip) and [here](https://stackoverflow.com/questions/5399706/how-i-can-call-a-javascript-function-with-monotouch-and-vice-versa/5407039#5407039) – ColeX Apr 10 '18 at 07:24

1 Answers1

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;
}
Michał Żołnieruk
  • 2,095
  • 12
  • 20
  • 1
    HI Michal, Thank you so much for your response for my question. – NagendraBabu Apr 10 '18 at 07:52
  • 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. – NagendraBabu Apr 10 '18 at 07:58
  • 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. – Michał Żołnieruk Apr 10 '18 at 08:23
  • @NagendraBabu that's great :) please accept my answer, so others can see that it's a good solution to this kind of problems – Michał Żołnieruk Apr 10 '18 at 11:27
  • 1
    i found one more best solution for calling c# method from javascript in UIWebview Xamarin iOS is from https://developer.xamarin.com/api/type/MonoTouch.JavaScriptCore.JSContext/ – NagendraBabu Apr 12 '18 at 07:43
  • Great answer too, @NagendraBabu – Tyler Jones Aug 29 '18 at 04:25