Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the final rendered height of a static page in a WKWebView

In my app I’m using a WKWebView which loads webpages with static content.

I want to know the height of the contentSize as soon as the webpage is fully rendered in the WKWebView.

So I thought I could use the webView:didFinishNavigation: delegate for that:

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

    @IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        webView.navigationDelegate = self
        webView.uiDelegate = self

        webView.load(URLRequest(url: URL(string: "https://www.rockade.nl/testpage/testpage.php")!))
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("finished loading: height=\(webView.scrollView.contentSize.height)")
    }

}

My problem with this code is that this delegate is called before the page is fully rendered, so I’m getting different results for the height, even values of 0.

When I add a small delay I’m getting the correct results, but that feels like a hack.

Using a observer like this

var myContext = 0
webView.scrollView.addObserver(self, forKeyPath: "contentSize", options: .new, context: &myContext)

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if context == &myContext {
            print(webView.scrollView.contentSize.height)
        } else {
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        }
    }
}

doesn’t help me too, because I really need to be sure that a received value is the final height.

Good to know: I don’t have control over the webpages, so using javascript is not an option.

I hope someone can point me into the right direction!

like image 570
Roel Koops Avatar asked May 29 '18 07:05

Roel Koops


1 Answers

So you don't have control over webpages but html dom is not changes on page to page. So you can use script to get the height of your page.

Maybe this helps to you:

        func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
            if complete != nil {
                webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
                    // Here is your height
                })
            }
        })
    }

PS: You can try 'document.body.offsetHeight' instead of scroll height also.

like image 157
U. Benlice Avatar answered Oct 13 '22 09:10

U. Benlice