1

I've written a function that's supposed to return the HTML string that makes up a WKWebview. However, the completion handler is never called, and the project freezes indefinitely. I've also already adopted the WKScriptMessageHandler protocol so that's not the problem.

public func getHTML() -> String {
    var result = ""
    let group = DispatchGroup()

    group.enter()

    DispatchQueue.main.async {
        self.webView.evaluateJavaScript("document.documentElement.outerHTML.toString()", completionHandler: {(html: Any?, error: Error?) in
            if (error != nil) {
                print(error!)
            }
            result = html as! String
            group.leave()
        })
    }

    group.wait()
    print("done waiting")
    return result
}

I've found several examples on how to get the html, like here, but I don't want to merely print, I want to be able to return its value. I'm not experienced with DispatchQueues, but I do know for that WKWebView's evaluateJavaScript completion handler always runs on the main thread

Maxwell Omdal
  • 172
  • 3
  • 14

0 Answers0