Reputation: 2864
I am working on hybrid ios application,i am trying to use wkwebview instead of uiwebview,because in wkwebview they fixed lot of performance issues over uiwebview,and the speed of loading also increased in wkwebview,if i am making any ajax request from supporting files ,i am getting Origin null is not allowed by Access-Control-Allow-Origin
code:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];
Upvotes: 6
Views: 9346
Reputation: 657
Taken from cordova-ios
in CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m after WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init]; add the following: [configuration setValue:@"TRUE" forKey:@"allowUniversalAccessFromFileURLs"];
Upvotes: 2
Reputation: 5303
Similar answer as the others, but the functions posted here didn’t ring the bell from a Cordova point of view which is what brought us here.
The underlying problem is that WKWebview expects local files to respect CORS as well, and basically you need to tell it to ignore those.
Using Cordova, you can fix this by adding:
[configuration.preferences setValue:@TRUE forKey:@"allowFileAccessFromFileURLs"];
[configuration setValue:@TRUE forKey:@"allowUniversalAccessFromFileURLs"];
to the createConfigurationFromSettings method in your CDVWebViewEngine.m file in your generated iOS project (normally under CordovaLib.xcodeproj/Private/CDVWebViewEngine/Plugins)
Someone created a plugin to add this: https://github.com/TheMattRay/cordova-plugin-wkwebviewxhrfix
But this is giving us compile problems (which are easy to fix but then also require a manual step..) so we are currently just adding the 2 lines ourselves to the CDVWebViewEngine.m file (adding iOS as a platform will remove the lines again, just doing a “cordova build ios” will not).
Upvotes: 1
Reputation: 1202
The next code solved the issue for me.
func loadWKWebview(){
let configs = WKWebViewConfiguration()
configs.setValue(true, forKey: "_allowUniversalAccessFromFileURLs")
let webView = WKWebView(frame: view.bounds, configuration: configs)
self.view.addSubview(webView)
let request = URLRequest(url: Bundle.main.url(forResource: "hello", withExtension: "html")!)
webView.load(request)
}
Pay attention that the configuration setup has to be done Prior to WKWebView instantiation
Upvotes: 5
Reputation: 6934
Like this in swift, but i'm sure the obj c equivalent is the same
webView.loadFileURL(urlIndex, allowingReadAccessTo: urlDir)
could also set the following - though i dont see this officially documented, and works for CORS errors for me but people seem to claim that it gets accepted in appstore :
webView.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
Upvotes: 3
Reputation: 4278
The source of your problem is in using [[NSBundle mainBundle] bundleURL]
as base URL. This URL is used in same origin policy checks for your HTML pages. Try to use nil
as base URL.
Note that passing nil
as base URL is insecure, so you should use something like [NSURL URLWithString:@“about:blank”]
if you care about security.
Upvotes: -1