Search code examples
iosswiftwkwebview

How can I load file with hash with WKWebview


I use the code to load URL path hash fragment (such as: index.html#/some-hash-path; but WKWebview not load url.

let path = "www/index.html"
let page = "#/some-hash-path"
let url = Bundle.main.bundleURL.appendingPathComponent(path + page)
self.webView.loadFileURL(url, allowingReadAccessTo: Bundle.main.bundleURL)

It seem that it convert # -> %23 which make invalid path


Solution

  • Construct URL from string work as expected:

    let path = "www/index.html"
    let page = "#/some-hash-path"
    guard let parsedUrl = URL(string: Bundle.main.bundleURL.absoluteString + path + page) else {
      return
    }
    self.webView.loadFileURL(parsedUrl, allowingReadAccessTo: Bundle.main.bundleURL)