Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS9 WKWebView Access-Control-Allow-Origin

Tags:

ajax

wkwebview

I load local html file on IOS WKWebview using loadFileURL: allowingReadAccessToURL.. But when I send ajax request, it fails.

The error is:

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

Can I set some properties to fix this error, like in Android,we can use setAllowUniversalAccessFromFileURLs?

like image 829
Haoru Zhan Avatar asked Nov 04 '15 06:11

Haoru Zhan


2 Answers

Have you tried setting it like this?

let config = WKWebViewConfiguration() config.setValue(true, forKey: "allowUniversalAccessFromFileURLs") 
like image 86
cmilhench Avatar answered Sep 18 '22 21:09

cmilhench


When HTML is loaded locally (file URL) with WkWebView below configuration setting to WkWebView worked for me

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; WKUserContentController *userContentController = [[WKUserContentController alloc] init];  [theConfiguration.preferences setValue:@"TRUE" forKey:@"allowFileAccessFromFileURLs"]; [theConfiguration setValue:@"TRUE" forKey:@"allowUniversalAccessFromFileURLs"]; 

When you set above setting, origin will be passed as "file://" instead of null

like image 21
Girish Adiga Avatar answered Sep 22 '22 21:09

Girish Adiga