Search code examples
javascriptiosajaxwkwebview

WKWebView xmlhttprequest with file url


I am migrating from UIWebView to WKWebView with my local HTMLs in the app documents folder. I can load the index page with all css and js files, but every ajax call (xmlhttprequest) fails because of allowed-access-origin.

I don´t want to use a webserver in my application, because I think it would be oversized. How can I make it possible? The app is a simple HTML5 application for an in house app. The device can´t get online or anything so security could be disabled completly.


Solution

  • This solved my problem:

    let config = WKWebViewConfiguration()
    config.userContentController = contentController
    config.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
    webView = WKWebView(frame: .zero, configuration: config)
    webView.uiDelegate = self
    webView.navigationDelegate = self
    view = webView