Can't properly get the height of WkWebView

koby1koby1 Member ✭✭
edited March 14 in Xamarin.iOS

I override the navigation delegate of WKWebView to know that the website is finished loading. I override the DidFinishNavigation method to get the height of the webview. I'm getting the height of the webview by EvaluateJavascript(). When I debug the code I sometimes get the height of the webview, but when I don't debug it, I don't totally get the height of the webview.

public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        {
            Thread.Sleep(2000);

            var javascriptString = "Math.max( " +
                "document.body.scrollHeight, " +
                "document.documentElement.scrollHeight)";


            DispatchQueue.MainQueue.DispatchAsync(() =>
            {
                webView.EvaluateJavaScript("document.readyState", (NSObject result, NSError err) =>
                {
                    if (result != null && result.ToString() == "complete")
                   {

                        webView.EvaluateJavaScript(javascriptString, (NSObject height, NSError err1) =>
                        {
                            if (height is NSNumber)
                            {
                                var x = (NSNumber)height;

                                if (x.FloatValue != 0)
                                {
                                    wkWebviewHeight = x.FloatValue;

                                    Console.WriteLine("Height retrieved");

                                    _controller.DisplayLoadingFinished(wkWebviewHeight);
                                }

                                Console.WriteLine((NSNumber)height);
                            }
                        });
                    }
                });
            });
        }

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited March 15

    Your code looks no problem , test on my side ,it works fine , i'v attached my testing sample .

  • koby1koby1 Member ✭✭

    @ColeX tried the solution you made and, it gets the height of the website. But I still can't get the height of the site I'm accessing. I used AngularJS to create the website I'm accessing on WKWebView.

  • ColeXColeX Member, Xamarin Team Xamurai

    @koby1 said:
    @ColeX tried the solution you made and, it gets the height of the website. But I still can't get the height of the site I'm accessing. I used AngularJS to create the website I'm accessing on WKWebView.

    Could you provide the website link so that i can test it on my side ?

  • koby1koby1 Member ✭✭

    @ColeX I can't give you the website because it requires a hash on the url to access it. The hash is generated from C# and the content is quite confidential.

Sign In or Register to comment.