在 WKWebView 中获取静态页面的最终呈现高度 [英] Getting the final rendered height of a static page in a WKWebView

查看:31
本文介绍了在 WKWebView 中获取静态页面的最终呈现高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我使用了一个 WKWebView 来加载包含静态内容的网页.

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

我想在 WKWebView 中完全呈现网页后立即知道 contentSize 的高度.

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

所以我想我可以使用 webView:didFinishNavigation: 委托:

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)")
    }

}

我对这段代码的问题是,这个委托在页面完全呈现之前被调用,所以我得到了不同的高度结果,即使值为 0.

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.

使用这样的观察者

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.

很高兴知道:我无法控制网页,因此不能选择使用 javascript.

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!

推荐答案

因此您无法控制网页,但 html dom 不会随页面变化.所以你可以使用脚本来获取页面的高度.

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.

也许这对你有帮助:

        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:您也可以尝试使用 'document.body.offsetHeight' 而不是滚动高度.

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

这篇关于在 WKWebView 中获取静态页面的最终呈现高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆