ios - iOS:如何在 WKWebView 中加载本地文件(不在包中)?
问题描述
我想使用 WKWebView(不是 UIWebView)并在其中加载一些 html 文件。我准确地说我在 info.plist 中将 ArbitraryLoads 设置为 YES。
(有关信息,它适用于模拟器但不适用于设备)。
<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 读取文件
NSURL *nsurl = [NSURL fileURLWithPath:[[[self documentsDirectory] stringByAppendingPathComponent:user_appli] stringByAppendingPathComponent:@"index.html"]]; //locally
NSURL *readAccessToURL = [[nsurl URLByDeletingLastPathComponent] URLByDeletingLastPathComponent];
[self.webView loadFileURL:nsurl allowingReadAccessToURL:readAccessToURL];
推荐阅读
- php - 流响应以下载 CSV 文件
- c# - 序列化/反序列化 jaon 数组键值对
- vuejs2 - 如何在 v-select 选项上设置属性?
- javascript - 如何让 Commando 机器人在消息中的任何位置使用特定子字符串响应消息
- python - 在 Python 中将列值连接成一列
- javascript - 使用 .set() 更新 Netsuite 记录上的字段不起作用
- python - 使用 2 个变量循环遍历 Python 列表
- java - 使用元组作为键的 Hashmap
- r - ggplot2:如何将点与小提琴图分组?
- vba - 如何选择具有最大值的记录并使用变量来引用记录集字段名称?