Skip to content Skip to sidebar Skip to footer

Ios Calculate Correct Wkwebview Height For Html String With Embeded Tweet

I am using wkwebview to load given HTML strings using loadHTMLString method. After wkwebview didFinishNavigation I am updating height of wkwebview to content height by evaluating j

Solution 1:

The best solution in this case would be to listening for changes on the "contentSize" with an observer.

In order to do that you will need the following 2 variables:

var observing =falseinternalvar webViewHeight: CGFloat=0 {
    didSet {
        guardlet webViewHeightConstraint =self.heightConstraint else { return }
        DispatchQueue.main.asyncAfter(deadline: .now() +0.5) {
            webViewHeightConstraint.constant =self.webViewHeight
            self.view.layoutSubviews()
            self.delegate?.view.layoutSubviews()
        }
    }
}

The asyncAfter method could be ignored, but in my case the size of the view was not well updated without it. And i was getting a blank space at the bottom.

The methods to start observe and listening for changes are the following:

funcstartObservingHeight() {
    let options =NSKeyValueObservingOptions([.new])
    webView?.scrollView.addObserver(self, forKeyPath: "contentSize", options: options, context: nil)
    observing =true
}

funcstopObservingHeight() {
    webView?.scrollView.removeObserver(self, forKeyPath: "contentSize", context: nil)
    observing =false
}

overridefuncobserveValue(forKeyPathkeyPath: String?, ofobject: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    guardlet keyPath = keyPath else {
        super.observeValue(forKeyPath: nil, of: object, change: change, context: context)
        return
    }
    switch (keyPath, context) {
    case("contentSize", nil):
        webViewHeight = (webView?.scrollView.contentSize.height)!default:
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
    }
}

As you can see, the variable webViewHeight will be updated when received a new value.

In your didFinish navigation method you should start "listening" for the observer calling the previous method startObservingHeight. This will start updating the height as far as the frames of the twitter are completed.

funcwebView(_webView: WKWebView,
             didFinishnavigation: WKNavigation!) {
    indicatorLoading?.stopAnimating()
    webViewHeight = webView.scrollView.contentSize.height
    webView.isHidden =falseif (!observing) {
        startObservingHeight()
    }
}

Solution 2:

//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(iOS8.0, *)


    funcwebView(_webView: WKWebView, decidePolicyFornavigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void){
        // If twitter feed finished renderinif(navigationAction.request.url?.scheme =="widget.twitter.myapp"){
            // Don’t update frame here
            decisionHandler(.cancel)
        }

        decisionHandler(.allow)
}

Post a Comment for "Ios Calculate Correct Wkwebview Height For Html String With Embeded Tweet"