WKWebView xmlhttprequest with file url

后端 未结 3 845
一向
一向 2021-02-14 17:12

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 (xmlhttprequ

3条回答
  •  执念已碎
    2021-02-14 17:13

    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.

提交回复
热议问题