My app uses WKWebView to present html string. WkWebView is embedded in a scrollView because i have several other elements (e.g. buttons, tableViews). Because of that i need to calculate the size of WkWebvView content and for that i use evaluateJavaScript(“document.body.scrollHeight”, completionHandler: method. Whenever i have embedded tweet in a html string, content height is wrongly calculated and my webview cuts a piece of its height.
Part of html string which has tweet embedded looks like this:
<blockquote class=\"twitter-tweet\" data-lang=\"en\"><p><a
href=\"https://twitter.com/hashtag/SYRIA?src=hash\">#SYRIA</a> Admiral Essen frigate launched <a href=\"https://twitter.com/hashtag/Kalibr?src=hash\">#Kalibr</a> cruise missiles against ISIS objects near Deir ez-Zor <a href=\"https://BLABLA*\">pic.twitter.com/azHAIii07g</a></p><p>— Минобороны России (@mod_russia) <a href=\"https://twitter.com/mod_russia/status/905007557554700288\">September 5, 2017</a></p></blockquote><p><script src=\"https://platform.twitter.com/widgets.js\"></script>
(BLABLA* is actually t.co/azHAIii07g which i had to replace because of stackoverflow rules)
Is there something missing in this twitter code, or should i set something in webview methods?
I tried implementing solution from this post: iOS Calculate correct WKWebview height for html string with embeded tweet but there was no success or at least i could figure out the answer.
Any advice is appreciated??
UPDATE: I hacked a little bit and found horrible workaround, but it is a good lead for where the problem lies. This is the full function which i use for calculating height:
`func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.webViewx.evaluateJavaScript("document.readyState", completionHandler: {
[weak self] (complete, error) in
if complete != nil { self?.webViewx.evaluateJavaScript("document.body.offsetHeight", completionHandler: {
[weak self] (height, error) in
})
}`
If i put:
`DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1, execute: {
self?.webViewx.evaluateJavaScript("document.body.offsetHeight", completionHandler: {
[weak self] (height, error) in
print("HEIGHT AFTER 1 sec", height)
})
})`
I get correct height after 1 second. Now, what would be the way to get that height update after correct time.
Also, one side note, if i have embedded facebook or instagram post, everything works as it should and webview height gets properly calculated.