Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebview Origin null is not allowed by Access-Control-Allow-Origin

Tags:

ios

wkwebview

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]];
like image 558
skyshine Avatar asked Sep 07 '16 05:09

skyshine


2 Answers

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

like image 109
Benny Davidovitz Avatar answered Oct 23 '22 09:10

Benny Davidovitz


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"];

like image 24
whodeee Avatar answered Oct 23 '22 11:10

whodeee