16

Apologies in advance if I've left out any relevant information in this question: I am not a web developer, so I'm not sure exactly what information is necessary to solve my problem.

I have a WKWebView that loads a website served locally, using code approximately like this (some details involving the manager are omitted, which I don't think are relevant to this question).

  WKWebViewConfiguration *webViewConfiguration = [[WKWebViewConfiguration alloc] init];
  WebViewManager *manager = [[WebViewManager alloc] init];
  webViewConfiguration.userContentController = manager;
  WKWebView *aWebView = [[WKWebView alloc] initWithFrame:self.view.bounds
                                           configuration:webViewConfiguration];
  [aWebView loadRequest:[NSURLRequest requestWithURL:aUrl]];
  [aWebView setNavigationDelegate:manager];

The HTML files for this website are stored in the App's bundle (a file:// URL), and need to be able to access various images, CSS files, JS files, and other resources that are stored in the same directory in the bundle (there are relative URLS in the HTML for this purpose, I believe).

I now need to be able to provide images from the App's shared container (as accessed by the containerURLForSecurityApplicationGroupIdentifier: method of NSFileManager).

However, the URLs I provide don't work. When I view page in the Safari Debugger, I see an exception is being thrown in some jQuery Code that involves loading files.

I've investigated a number of possible workarounds, but none of them have solved my issue:

The new loadFileURL:allowingReadAccessToURL: method appeared to be just what I need, however, setting the readAccessURL parameter to the folder in my shared security container broke the website -- indeed, it didn't even look like the HTML files themselves loaded properly, as none of them had the right names in the Safari Debugger. As a test, I set the URL to the URL in the bundle of the directory containing the HTML pages, and got a different error.

Another developer suggested that we convert each image from outside the bundle into a base64-encoded string and pass it into the website via Javascript, but that seems terribly inefficient to me, so I'm hoping to avoid that outcome.

Finally, I've read elsewhere that copying files into the tmp/www directory of the app solves similar issues to this on iOS 8; so I wonder if copying everything into the same directory (outside of the bundle, which of course is read-only) might fix this. However, this too seems grossly inefficient.

How can I load files both from inside the bundle and in the shared application container into my web view?

Ben Pious
  • 4,765
  • 2
  • 22
  • 34
  • 1
    Try using `aWebView.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")` – Jacob Boyd Jun 12 '17 at 18:18
  • Hi @JacobBoyd have you tried it on iOS9? it seems to not work, I'm using Xcode 9.4.1, it works on the latest version, but I wanted to try and support upto iOS 9 – Vincent Bacalso Oct 09 '18 at 05:41
  • Did you ever get a solution. I recently achieved getting local images into a WKWebView using PNG data, base64 encoding and HTML string substitution. It was in Swift though - I don’t know how to write it in Obj-C. – Chris Feb 07 '19 at 13:33

0 Answers0