IOS10 WKWebview - Evaluation JavaScript ("document.height") return nil

In my application, I need to get the webpage height in WKWebView. Therefore, I use the code below.

webView.evaluateJavaScript(("document.height"), completionHandler: { contentHeight,error in
    print(contentHeight)
})

Prior to iOS 9, this will return the height of the webpage correctly. But in iOS 10 contentHeightalways nil.

I have installed

webView.scrollView.scrollEnabled = false

and the web view is in UITableViewCell.

Height webViewand UITableViewCellare variable. There is an upper and lower restriction on the web view to match the cell.

When I get the height of the webpage, I want it to be reflected in the height of the cell.

+4
source share
2 answers

try the following:

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

    webView.evaluateJavaScript(javascriptString) { (result, error) in
        if error == nil {
            if let result = result, let height = JSON(result).int {
                self.htmlContentHeight = CGFloat(height)
                self.resetContentCell()
            }
        }
    }

PS. WKWebiew: WKWebView iOS 10

+8

webView.evaluateJavaScript(("document.body.offsetHeight"), completionHandler:^(id _Nullable result, NSError * _Nullable error) {
    NSLog(@">>>%@",result);
})
0

Source: https://habr.com/ru/post/1654947/


All Articles