0

I would like to use a WKWebView (not a UIWebView) and load some html files in it. I precise that I put the ArbitraryLoads to YES in info.plist.

(For info, it works on simulator but not on device).

<key>NSAppTransportSecurity</key>
<dict>
      <key>NSAllowsArbitraryLoads</key>
        <true/>
</dict>

Here is my code:

       WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
    [theConfiguration setMediaPlaybackAllowsAirPlay:YES];
    [theConfiguration setAllowsInlineMediaPlayback:YES];
    [theConfiguration setAllowsPictureInPictureMediaPlayback:YES];
    [theConfiguration setAllowsAirPlayForMediaPlayback:YES];
       [theConfiguration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
[theConfiguration setValue:@YES forKey:@"_allowUniversalAccessFromFileURLs"];
       self.webView.navigationDelegate = self; 
       self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];

      NSURL *nsurl2 = [NSURL fileURLWithPath:[[[self documentsDirectory] stringByAppendingPathComponent:user_appli] stringByAppendingPathComponent:@"index.html"]];//locally


 [self.webView loadFileURL:nsurl2 allowingReadAccessToURL:nsurl2]; //=> doesn't work  

 [self.webView loadRequest:[NSURLRequest requestWithURL:nsurl2]]; //=> doesn't work

Thanks in advance.

ΩlostA
  • 2,501
  • 5
  • 27
  • 63

1 Answers1

1

You have to use the root of the directory to allow the webview to read the file

NSURL *nsurl = [NSURL fileURLWithPath:[[[self documentsDirectory] stringByAppendingPathComponent:user_appli] stringByAppendingPathComponent:@"index.html"]]; //locally
NSURL *readAccessToURL = [[nsurl URLByDeletingLastPathComponent] URLByDeletingLastPathComponent];

[self.webView loadFileURL:nsurl allowingReadAccessToURL:readAccessToURL];
dt dino
  • 1,194
  • 6
  • 19