Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView cannot trigger ajax to load a file on local

I embed all html in WKWebView, all works until I recognize that WKWebView cannot load a xml file on the local


$.ajax({
        type: "GET",
        url: "tags.xml",
        dataType: "xml",
        cache: false,
        success: function(xml) {

        },
        error: function() {

            alert("An error occurred while processing XML file.");
        }
    });

my code for UIWebView

//urlFolder is located locally in a temporary file: tmp/www/htmlFolder 
//urlFile is located in the urlFolder: tmp/www/htmlFolder/index.html
//xml file is located in the urlFolder: tmp/www/htmlFolder/tags.xml


WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
    _webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
    [_webView loadFileURL:urlFile allowingReadAccessToURL:urlFolder];

    [self.view addSubview:_webView];

Note: I am using XCode7.1 Beta, Objective-C, ios9.1, WKWebView

like image 897
chipbk10 Avatar asked Oct 19 '15 09:10

chipbk10


1 Answers

From what I could find they disabled cross-origin requests in WKWebViews.

Search for cors or xhr + WKWebView for more information about this issue. I think it must be a bug of some sort, because this has always been possible in 'normal' UIWebViews using local files (like your example).

You can however run a small/lightweight http-server inside your app, which works great for me. Make sure to add an exception to the App Transport Security Settings in your .plist file for localhost.

like image 151
Dirk de Boer Avatar answered Jan 03 '23 13:01

Dirk de Boer