7

I am having some issues when trying to run my app on an iPad (or any device) it runs as expected on the emulator so it is weird that it doesn't work on a device. I was wondering if some one could point me in the correct direction. I spend many hours reading all the other posts on here about the same issues, however none of the suggested solutions worked.

I have a WKWebView into which I am loading a local html file. On the emulator the file loads and everything works fine but on a device I am getting a message in the log:

Could not create a sandbox extension for '/'

Here is the code I have that loads the file into the

override func viewDidLoad() {
    super.viewDidLoad()
    var path = NSBundle.mainBundle().pathForResource("Login_UK",
        ofType: "html")
    var url = NSURL(fileURLWithPath: path!)
    var request = NSURLRequest(URL: url!)

    var theConfiguration = WKWebViewConfiguration()
    theConfiguration.userContentController.addScriptMessageHandler(self,
        name: "callbackHandler")

    webView = WKWebView(frame: self.view.frame,
        configuration: theConfiguration)
    webView!.loadRequest(request)
    self.view.addSubview(webView!)
}

Any help will be greatly appreciated

Kind Regards, Dimitar

Dimitar Dyankov
  • 209
  • 1
  • 3
  • 10
  • possible duplicate of [WKWebView not working in iOS 8 beta 4](http://stackoverflow.com/questions/24882834/wkwebview-not-working-in-ios-8-beta-4) – Evan Kroske Aug 09 '15 at 00:35
  • See also the discussions at https://stackoverflow.com/questions/24882834/wkwebview-not-working-in-ios-8-beta-4 for other workarounds, plus good news for iOS 9 – Marc Durdin Jul 28 '15 at 00:09

4 Answers4

8

Thank you for any one who tried to answer my question. I have released that this is an error with the WebKit lib that Apple are trying to fix. However I have found a good workaround that required little work.

I open the local file and read its content and then send that string into a webView.loadHTMLString method that compiles the hmtl that was in the file. That way you avoid the issues with iOS not being able to find the path to the local file.

Here is an example of reading a file and then opening it for any one who has the same issues:

    let path2 = NSBundle.mainBundle().pathForResource("index", ofType: "html")
    var text = String(contentsOfFile: path2!, encoding: NSUTF8StringEncoding, error: nil)!

    webView!.loadHTMLString(text, baseURL: url)

Kind regards, Dimitar

Dimitar Dyankov
  • 209
  • 1
  • 3
  • 10
5

Just do this:

if url.isFileURL {
    webView.loadFileURL(url, allowingReadAccessTo: url)
} else {
    let request = URLRequest(url: url)
    webView.load(request)
}
drewster
  • 5,460
  • 5
  • 40
  • 50
3

There is a function loadFileURL on the WKWebView starting iOS 9 that apparently has to be used when reading data from a file URL.

Strange enough using the load function with an URLRequest for the file URL does work in the simulators, but not on device - the web view stays blank on the device. Using the loadFileURL works on device and the simulator.

Using loadHTMLString unfortunately introduces another problem (local anchors that jump to another position in the same web view are not working anymore) and probably should be avoided until Apple releases a fix for that issue.

TheEye
  • 9,280
  • 2
  • 42
  • 58
3

Actually the problem is caused by the webView.load() function, if we test it on simulators it will work perfectly, but for the real device it may cause some problems and it will not load the webview perfectly. You may check it by calling the didFinish() function. What you need to do is call webView.loadFileURL() rather than webView.load(). It will work in both simulators and real devices. This is very useful when you load any file from the local file directory.

Juan Carlos Ramirez
  • 2,054
  • 1
  • 7
  • 22
nirojan
  • 159
  • 2
  • 3
  • 1
    webView.loadFileURL() too not working self.webView?.loadFileURL(baseURL!, allowingReadAccessTo: baseURL!) Any suggestions how you solved this issue https://stackoverflow.com/questions/63800637/unable-to-load-html-with-only-images-in-wkwebview-ios-13-swift-5 – iMinion Sep 09 '20 at 10:13