0

I'm having troubles with the following code. It's supposed to load a local HTML file into a WKWebView.

It works fine on any simulator I've tried (ios 12 / ios 13).

For a reason I do not understand yet, it does not work on device. Instead, I get a blank screen. Debugging the webview with the Safari debugger shows that the page is "about:blank". It did not load my file... Why ?

Note that I have checked the existence of the file by downloading the app container from the phone and the file is there...

- (void)loadIndexConteneurWithHash:(NSString *)hash
{
    NSString *fileName = @"index.html";
    NSString *subpath = hash ? [NSString stringWithFormat:@"%@#%@", fileName, hash] : fileName;
    NSString *rootContainerDirectoryPath = [NSFileUtility pathRelativeToContentDirectoryForSubpath:@"/www/v2"];

    NSURL *URL = [NSURL URLWithString:subpath relativeToURL:[NSURL fileURLWithPath:rootContainerDirectoryPath]];

    [[NSURLCache sharedURLCache] removeAllCachedResponses];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];

    [self.webView performSelectorOnMainThread:@selector(loadRequest:) withObject:request waitUntilDone:true];
}

Edit: this question is NOT a duplicate of Load local web files & resources in WKWebView

I do not load my HTML file from the bundle but from the app container as noted originally in my question. I've also tried the loadFileURL method and it does not work either.

Żabojad
  • 2,946
  • 2
  • 32
  • 39
  • What is this `[NSFileUtility pathRelativeToContentDirectoryForSubpath:@"/www/v2"]` method supposed to do ? – CZ54 Oct 17 '19 at 15:44
  • @CZ54 it gives the absolute path for given path relatively to `/var/mobile/Containers/Data/Application/<...>/Library/Application Support//LocalContents` – Żabojad Oct 17 '19 at 15:47
  • What delegate method tells? Ok or error? – Cy-4AH Oct 17 '19 at 15:47
  • @Cy-4AH the `decidePolicyForNavigationAction` is raised and I allow it. But neither the `didFailNavigation` nor the `didFinishNavigation` are called... – Żabojad Oct 17 '19 at 16:00
  • When looking at the duplicate, note that the currently accepted answer shows that you need to call `loadFileURL` and `load`. That 2nd call to `load` is not needed. You just need `loadFileURL`. – rmaddy Oct 17 '19 at 16:03
  • great @rmaddy you've marked that question as a duplicate when I've already looked at the one you've pointed and it does not resolve my problem... I've tried `loadFileURL` and that does not work too... Please un-mark my question, it is not a duplicate. – Żabojad Oct 17 '19 at 16:10
  • The use of `loadFileURL` is what works when loading a local file so it is a duplicate. The use of `loadFileURL` does not require the file to be from the bundle. It works with any local file when used correctly. As shown in the close message: *"This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question."*. So please post a new question showing your attempt to use `loadFileURL` and clearly provide all relevant details about the URL you attempt to load and any other relevant details. – rmaddy Oct 17 '19 at 16:21

1 Answers1

-1

In order to access a Local ressource you have to use : Bundle.main.url(forResource: "MyFile", withExtension: "html")

CZ54
  • 5,488
  • 1
  • 24
  • 39