iOS - Wkwebview Cross Origin Requests Are Only Supported for Http

iOS - WKWebView Cross origin requests are only supported for HTTP

To resolve it, you need to turn allowFileAccessFromFileURLs on from WKPreferences

Swift

webView.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")

Objective-C

[webView.configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];

WkWebview local site Access Origin

I tried the exact same code with your folder structure and this worked for me so I think this error originates from the html content.

Try and see if any of these help you:

Set he allow read access on the directory

// I added
let directoryURL = Bundle.main.resourceURL!.appendingPathComponent("iosbuild")

// your code
let url = Bundle.main.url(forResource: "index",
withExtension: "html",
subdirectory: "iosbuild")!

// changed read access
webView.loadFileURL(url, allowingReadAccessTo: directoryURL)

This could be CORS related, so add a webview configuration

// Configure this before instantiating web views
// Worked for me in the past with CORS errors
let configs = WKWebViewConfiguration()
configs.setValue(true, forKey: "_allowUniversalAccessFromFileURLs")

let webView = WKWebView(frame: view.bounds, configuration: configs)
self.view.addSubview(webView)

// rest of your delegate set up
webView.navigationDelegate = self
webView.uiDelegate = self
webView.configuration.defaultWebpagePreferences.allowsContentJavaScript = true

// your code
let url = Bundle.main.url(forResource: "index",
withExtension: "html",
subdirectory: "iosbuild")!

// this is back to your own code but if it doesn't work
// you can try allowing the read access on the directory like before
webView.loadFileURL(url, allowingReadAccessTo: url)

Give these a go and see if this gives you any luck

Cordova 6 app cannot load from file:// path

EDIT:

The ionic team has been working on a fork of the WKWebViewEngine plugin and has fixed some of the XHR issues, not sure if this one is fixed. You can take a look on https://github.com/driftyco/cordova-plugin-wkwebview-engine

Old answer:
This is a known issue, local file:// url XmlHttpRequests are not allowed in WKWebViewEngine (https://issues.apache.org/jira/browse/CB-10143)

For more known issues see https://issues.apache.org/jira/browse/CB-10237?jql=labels%20%3D%20wkwebview-known-issues

If you want to make local ajax call to work you should use the wkwebview-engine-localhost plugin (https://github.com/apache/cordova-plugins/tree/master/wkwebview-engine-localhost)

Or Telerik's WKWebView plugin (https://github.com/Telerik-Verified-Plugins/WKWebView).

Both of them will use a local webserver that solves some of the known WKWebView limitations.



Related Topics



Leave a reply



Submit