I have a wkWebView included in Table View Cell, that is self resizing and is filled via a pageable controller. It can be enabled and disabled depending on html string that is delivered or not. The cell is designed programmatically.
I have set up everything as seen on many examples seen on the web., but it doesn't show anything, just plain white space. I have no Idea, what I'm doing wrong. Maybe someone can give me a hint.
Here are the code parts concerning the webview:
private let webView:WKWebView = { () -> WKWebView in
let webConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
//webView.navigationDelegate = self
webView.translatesAutoresizingMaskIntoConstraints = false
return webView
}()
func update(entity: Codable) {
guard let news = entity as? NewsStream else {
print("not a news stream!")
return
}
if news.category == nil {
news.category = news.widgetType
}
//Set global news variable
self.news = news
createWebView()
}
func createWebView(){
webView.isHidden = true
if let html = news?.html, let link = news?.webLink {
if !html.isEmpty{
webView.isHidden = false
// let html2 = "<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>"
webView.loadHTMLString(html, baseURL: Bundle.main.bundleURL)
// let myURL = URL(string: "https://www.apple.com")
// let myRequest = URLRequest(url: myURL!)
// webView.load(myRequest)
webView.frame = CGRect(x: 0, y: currentYPos, width: contentView.frame.width, height: 400)
currentYPos = currentYPos + webView.frame.height
webView.backgroundColor = UIColor.green
webView.tintColor = UIColor.orange
webView.navigationDelegate = self
}
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
self.webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
self.webView.frame = CGRect(x: 0,
y: self.webView.frame.origin.y,
width:self.webView.frame.width,
height: height as! CGFloat)
})
}
print("Webview Did load")
print(webView.frame)
})
}
Try to use this.
if let bodyText = news.bodyText {
myWebview.loadHTMLString(bodyText, baseURL: nil)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.