1

While working on a web app for weeks, I used a external URL in my (WKWebView) web view. Now I'm moving towards production I want to embed the webapp and load a local webpage.

I simply moved from

let url = URL(string: "http://hidden-url.com/")
self.webView!.load(URLRequest(url: url!))

To

let url = URL(string: Bundle.main.path(forResource: "index_prod", ofType: "html")!)
self.webView!.load(URLRequest(url: url!))

But this causes my app to crash. I'm sure the file is loaded correctly and a print before, in-between and after the lines will appear in the console.

The error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

1

You need to load the string content from the file, use contentsOfFile method to get the string and use loadHTMLString method of webview, print the error or create some enum which shows the error

enum WebError: Swift.Error {
    case fileNotFound(name: String)
    case parsing(contentsOfFile: String)
}

guard let url = Bundle.main.path(forResource: "index_prod", ofType: "html") else {
   throw WebError.fileNotFound(name: file) // throw file not found error 
}

do {
      let html = try String(contentsOfFile: url)
      self.webView.loadHTMLString(html, baseURL: Bundle.main.bundleURL)
} catch {
      throw WebError.parsingError(contentsOfFile: file) 
}
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60