Swift WKWebView Loading local file not working on device
I am having some problems when trying to launch an app on iPad (or any device) that is running on the emulator as expected, so it's strange that it doesn't work on the device. I was wondering if anyone could point me in the right direction. I spend many hours reading all the other posts here about the same problems, however none of the suggested solutions worked.
I have a WKWebView into which I load a local html file. On the emulator the file is downloaded and everything works fine, but on the device I get a message in the log:
Failed to create sandbox extension for '/'
Here is the code I have that loads the file into
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 would be greatly appreciated
Regards, Dimitar
source to share
Thanks for everyone who tried to answer my question. I posted that this is a bug with the WebKit lib that Apple is trying to fix. However, I found a good workaround that required a little work.
I open a local file and read its contents and then I send this string to the webView.loadHTMLString method which compiles the hmtl that was in the file. This way you avoid problems with iOS that cannot find the path to the local file.
Here's an example of reading a file and then opening it for anyone with the same problems:
let path2 = NSBundle.mainBundle().pathForResource("index", ofType: "html")
var text = String(contentsOfFile: path2!, encoding: NSUTF8StringEncoding, error: nil)!
webView!.loadHTMLString(text, baseURL: url)
Regards, Dimitar
source to share
As of WKWebView
iOS 9, there is a function loadFileURL
that seems to be used when reading data from a file url.
Strangely, using a function load
with URLRequest for a file url does the job in simulators, but not on the device - the web view remains empty on the device. Usage loadFileURL
works on device and simulator.
Usage loadHTMLString
unfortunately leads to another issue (local bindings that go to a different position in the same web view no longer work) and should probably be avoided until Apple releases a fix for this issue.
source to share
Actually the problem is caused webView.load()
, if we test it on simulators it will work fine, but for a real device it might cause some problems and won't load the web view perfectly. You can check this by calling didFinish()
. What you need to do is call webView.loadFileURL()
and not webView.load()
. It will work on both simulators and real devices. This is very useful when you are loading any file from your local file directory.
source to share
See also WKWebView discussion not loading local files under iOS 8 for other workarounds, and good news for iOS 9
source to share