Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 469
AlteGurke Avatar asked Feb 22 '16 13:02

AlteGurke


People also ask

How do I use WKWebView?

Here's how: Open the XIB or Storyboard you want to add the web view to in Interface Builder. Find the web view or WKWebView in the Object Library at the bottom-left of Interface Builder. Drag-and-drop a WKWebView object from the Object Library to your view controller's canvas, and adjust its size and position.

How do I migrate to WKWebView?

Let's start integrating WKWebview in our project storyboard” and on your ViewController's view drag “Webkit View”. Select a WKWebView and place it on your view of a view controller. Step-3 : Now, open ViewController class and import Webkit then create IBOutlet for “WKWebview”.

What is WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


2 Answers

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
like image 54
Anders Drevin Avatar answered Oct 12 '22 09:10

Anders Drevin


For those who came though SO to find this topic:

This isn't official to turn off the security in WKWebView, but we could use the private API to allow this just like this guy did for the Cordova project: cordova-plugin-wkwebviewxhrfix

The clue is to create the configuration for the WebView and set the allowFileAccessFromFileURLs property.

WKWebViewConfiguration* configuration = originalImpSend(_self, selector, settings);

// allow access to file api
@try {
    [configuration.preferences setValue:@TRUE forKey:@"allowFileAccessFromFileURLs"];
}
@catch (NSException *exception) {}

@try {
    [configuration setValue:@TRUE forKey:@"allowUniversalAccessFromFileURLs"];
}
@catch (NSException *exception) {}

return configuration;

But as I mentioned this is the private API and it could be a reason to reject your application in Apple's App Review. If you want to publish your app in App Store, please consider to run some lightweight HTTP server instead of violate overall security of the web view. Example: GCDWebServer.

like image 21
Michal Cichon Avatar answered Oct 12 '22 08:10

Michal Cichon