iOS:如何在 WKWebView 中加载本地文件(不在包中)? [英] iOS: How to load local files (not in the bundle) in a WKWebView?
本文介绍了iOS:如何在 WKWebView 中加载本地文件(不在包中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 WKWebView(不是 UIWebView)并在其中加载一些 html 文件.我确切地说我在 info.plist 中将 ArbitraryLoads 设置为 YES.
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.
(有关信息,它适用于模拟器,但不适用于设备).
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
这是我的代码:
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
提前致谢.
推荐答案
必须使用根目录才能让webview读取文件
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];
这篇关于iOS:如何在 WKWebView 中加载本地文件(不在包中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文