0

I am pulling in HTML content via an API that loads JSON and would like to use WKWebView in order to display it. Is it possible to use WKWebView without a URL?

This controller is built programmatically, so the webView is added as Subview. In the long run I will have the webView display below other content that is also pulled from the API.

This is the code I have:

var webView = WKWebView()
webView = WKWebView(frame: scrollView.bounds, configuration: WKWebViewConfiguration())
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

scrollView.addSubview(webView)
webView.navigationDelegate = self as? WKNavigationDelegate
webView.loadHTMLString("<html><body>\(self.articleBody)</body></html>", baseURL: nil)

I get this error when I load my app:

Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service

In testing with a random URL and no HTML it works just fine.

I've looked at this question, but it doesn't have an answer that makes sense for me: com.apple.WebKit.WebContent drops 113 error: Could not find specified service

cjochum
  • 79
  • 2
  • 7

1 Answers1

0

I'm still getting the same error, but I am at least seeing my content now ...

The issue had to do with where I was calling the webView.loadHTMLString. I had to move it inside of my URLSession code instead of keeping it outside.

URLSession.shared.dataTask(with: url) { (data, response, err) in
   guard let data = data else { return }

   if err == nil {
     do {
       let decoder = JSONDecoder()
       let devo = try decoder.decode(Devotion.self, from: data)

       DispatchQueue.main.async {
          self.articleBody = devo.body
          self.webView.loadHTMLString("<html><body>\(self.articleBody)</body></html>", baseURL: nil)
       }
     } catch let err {
        print("Err", err)
     }
   }
}.resume()
cjochum
  • 79
  • 2
  • 7