Setting "Disable web security" and "allow file access from files" in iOS WKWebView

14,130

It's not possible to disable web security in WKWebView - there's no preference to do so - see the iOS source code for WebKit preferences.

There is a way to allow access from file URLs, although it's not officially supported. In the source code, a preference exists, so you can set it like this:

[wkWebView.configuration.preferences setValue:@TRUE forKey:@"allowFileAccessFromFileURLs"];

This enables access to file URLs relative to the content src. e.g. if the local page is foo/bar/index.html you can access files in foo/bar/ (e.g. foo/bar/1.jpg or foo/bar/sub/2.jpg) but not outside (e.g. foo/other/3.jpg or Documents/NoCloud/4.jpg).

Share:
14,130
Huanrong
Author by

Huanrong

Updated on June 21, 2022

Comments

  • Huanrong
    Huanrong about 2 years

    How to disable web security in iOS WKWebView? I used command "open /Applications/Google\ Chrome.app --args --disable-web-security --allow-file-access-from-files" in mac system to open the chrome. But How to do this in WKWebView? Thankes!