user132397
user132397

Reputation: 61

Execute Javascript in Xamarin.Forms WebView

im trying to execute Javascript in my Xamarin.Forms WebView by using the tutorial described here:

https://www.xamarinhelp.com/xamarin-forms-webview-executing-javascript/

Android works really good but I have problems setting this up on iOS. In the iOS custom renderer I basically return the js execute result by

return Task.FromResult(this.EvaluateJavascript(js));

Since last year the iOS api changed and this line throws an error when debugging. It turns out that I should use WkWebViewRenderer instead of WebViewRenderer. I tried to change but EvaluateJavascript() method now wants two arguments... one is the js string and one is a WKJavascriptEvaluationResult handler ...

I dont know how to adapt the code in the tutorial... can someone help?

Upvotes: 2

Views: 5617

Answers (2)

user132397
user132397

Reputation: 61

I just changed

return Task.FromResult(this.EvaluateJavascript(js));

to:

var x = await webView.EvaluateJavascriptAsync(js);
return x;

and now it works

Upvotes: 2

nevermore
nevermore

Reputation: 15786

It should be something like this:

    WKJavascriptEvaluationResult handler = (NSObject result, NSError error) => {
        if (error != null)
        {
            Console.WriteLine(result.ToString());
        }
    };
    webView.EvaluateJavaScript(JSCode, handler);
    

Upvotes: 0

Related Questions