locked
Can't properly get the height of WkWebView RRS feed

  • Question

  • User378870 posted

    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);
                                }
                            });
                        }
                    });
                });
            }
    
    Thursday, March 14, 2019 8:51 AM

All replies

  • User369978 posted

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

    Friday, March 15, 2019 9:39 AM
  • User378870 posted

    @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.

    Monday, March 18, 2019 2:26 AM
  • User369978 posted

    @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 ?

    Tuesday, March 19, 2019 9:36 AM
  • User378870 posted

    @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.

    Wednesday, March 20, 2019 6:31 AM