iOS使用embedded式tweet计算htmlstring的正确WKWebview高度

我正在使用wkwebview使用loadHTMLString方法加载给定的HTMLstring。 wkwebview didFinishNavigation后,我正在通过评估JavaScript“document.height”更新内容高度的wkwebview的高度。 这是纯HTMLstring的工作。

但对于embedded式tweet高度计算的htmlstring出错。 这里是鸣叫内容

</p> <blockquote class="twitter-tweet"> <p lang="fr" dir="ltr">Union Cabinet approves civil aviation policy.</p>&mdash; Press Trust of India (@PTI_News) <a href="https://twitter.com/PTI_News/status/742990663495647232">June 15, 2016</a></blockquote> <p> <script>window.twttr = (function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0],t = window.twttr || {};if (d.getElementById(id)) return t;js = d.createElement(s);js.id = id; js.src = "https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js, fjs);t._e = []; t.ready = function(f) {t._e.push(f);};return t;}(document, "script", "twitter-wjs"));</script> 

所以在webview完全加载twitter feed之后,我们不会在wkwebviewdelegate方法中获得callback。

  //Add some Scheme tag in your HTML string. let urlwithScheme = 'widget.twitter.myapp://url' let strTwitter = "<script> twttr.ready(function(e){ twttr.events.bind('rendered', function (event) { window.location = \(urlwithScheme); } ); }) </script>" let strHTML = "<!DOCTYPE html><html>\(strTwitter)</html>" // After load HTML check url scheme, if it belongs to twitter rendered action or not @available(iOS 8.0, *) func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void){ // If twitter feed finished renderin if(navigationAction.request.url?.scheme == "widget.twitter.myapp"){ // Don't update frame here decisionHandler(.cancel) } decisionHandler(.allow) }