Forum Xamarin Xamarin.iOS

How to execute a Javascript function inside WKWebview?

abitzanabitzan Member ✭✭

Hello community,

I am stuck with this problem for days and I couldn't find any solutions, maybe you can help me out..

I created an App that uses the WKWebview to connect to a website. The login happens native on iOS, after the user issued their credentials, i want to Login on the webpage as well.

Therefore my Webpage has a Javascript function called "handleLogin" where i receive the credentials and the user does not have to login again and is directly fowarded to his main page.

Now I want to call handleLogin() from native code inside my WKWebview.

I already tried several ways, on Android it can easily be done with:


In iOS I already tried it inside
public override async void DidFinishNavigation(WKWebView webView, WKNavigation navigation)

await webView.EvaluateJavaScriptAsync("handleLogin(credentialsPlaceholder);");

but the only result i get is an exception:

Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=0, WKJavaScriptExceptionMessage=TypeError: undefined is not a function, WKJavaScriptExceptionColumnNumber=0, NSLocalizedDescription=A JavaScript exception occurred}'

Can it be that I have to wait longer until everything is loaded?
I am sure that handleLogin exists, testet it on Android as well as in browsers, how can it be undefined?
Does EvaluateJavascript work only on internal scripts?

I hope you can help me out,
Kind regards


  • LandLuLandLu Member, Xamarin Team Xamurai

    This error is thrown by the javascript method. I noticed we need to pass an argument in this method. If you used the wrong type this method will fail.
    I created a simple project to help you understand this process. Here is the html:

    And I try to call this method in native like:

    public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        string message = "Test";
        webView.EvaluateJavaScript($"changeText('{message}');", (result, error) =>

    The h1 will change its text to Test. However, if I use the wrong type of parameter an issue which has the same exception will occur. I attached my sample here for you referring.

  • abitzanabitzan Member ✭✭

    Thank you for your response. In my case, I used the right parameters, I just replaced them here in the forum since the spam detector was not happy with my $ variables..
    Yes, this method is thrown by the javascript method, but if I debug it inside Safari with the same parameters it works.
    Is there a way to find out if this function exists from native c# code?

    Thank you :)

  • LandLuLandLu Member, Xamarin Team Xamurai

    Could you please make a webpage which contains this js method with the same parameters? And then consume it in a repo which could help us reproduce this issue.
    We don't need the real functionality of that method so you could make it a simple one. At last, post it here so that I can look into it to debug this issue on my side.

Sign In or Register to comment.