skip to Main Content

I have added a WebView to load HTML and the baseUrl method. But it leads to crashing the app and showing a warning

This method should not be called on the main thread as it may lead to
UI unresponsiveness.

//load HTML
let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
let folderPath = Bundle.main.bundlePath
let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true)
do {
    let htmlString = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue)
    self.webView.loadHTMLString(htmlString as String, baseURL: URL(string: newBaseURL))
} catch {
    // catch error
    print(error.localizedDescription)
}
                          

I have called this piece of code in viewDidLoad(). Also it has been added in dispatch queue. Any help is much appreciated.

3

Answers


  1. Call loadHTMLString from the main thread.

    DispatchQueue.main.async {
          self.webView.loadHTMLString(htmlString as String, baseURL: URL(string: newBaseURL))
    
    }
    
    Login or Signup to reply.
  2. You have 2 issues and one of them is a bug address by Apple itself –

    a. Warning of WKWebkit on main thread – This has been covered by one of Apple’s engineers where he goes into detail on this thread. Short answer – Its a bug and Apple is working on it in further releases. Now according to them this is supposed to be a warning and it shouldnt have any issues with the API itself.

    b. The Webview is unresponsive. As explained above the warning shouldn’t be causing an issue with anything API related. So for this I would suggest you to check the HTML you are trying to render separately. If that works fine, maybe try to launch it in a separate device, or even simulator. Try to make the HTML render on a consistent basis in other environment(device / simulator). I think either the device you are using has an older OS.

    Please try to test the same HTML on separate devices / simulators and share your results here.

    Login or Signup to reply.
  3. To fix this issue, you can move the code that loads the HTML string and sets the baseURL to a background thread using DispatchQueue. This will allow the main thread to continue running and handling user input, preventing the app from becoming unresponsive. Checkout the below code:

    DispatchQueue.global(qos: .userInitiated).async {
        let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
        let folderPath = Bundle.main.bundlePath
        let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true)
        do {
            let htmlString = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue)
            DispatchQueue.main.async {
                self.webView.loadHTMLString(htmlString as String, baseURL: URL(string: newBaseURL))
            }
        } catch {
            // catch error
            print(error.localizedDescription)
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search