WKWebView 从 Bundle 和 Document 目录加载文件
WKWebView load files from both Bundle and Document directory
我是 运行 一个使用 WKWebView 的网络应用程序,它从名为 [=31= 的 Bundle 目录加载内容和资产(html、js、img、css...) ]Web_Assets。
我这样加载 index.html,并将 Web_assets 设置为 baseURL:
if let filePath = Bundle.main.path(forResource:"index", ofType:"html", inDirectory: "Web_Assets") {
let html = try String(contentsOfFile: filePath, encoding: .utf8)
webView.loadHTMLString(html, baseURL: Bundle.main.resourceURL?.appendingPathComponent("Web_Assets"))
}
这完美地工作:index.html 被加载并且 Web_Assets 作为其他资源的基本路径没有任何问题。
我也可以使用等效的 loadFileURL 方法(根据我的理解)。
但是,现在我想在我的 WKWebView 中使用来自 Document 目录的其他资源(在我的例子中是视频),该目录是静态的并且可以通过 iTunes 文件共享进行管理,而当然保持当前架构不变(Bundle 目录中的 Web 资产)。
例如,我的页面 index.html 会从 Web_Assets 加载他的 style.css 和其他资产并显示 HTML5 视频位于 文档
可以这样做吗?
想法(我不知道如何实现):
- Web_assets 中文档的符号链接?
- WKWebView 中不同文件类型的不同 baseURL?
(将所有请求路由到 Web_Assets 除了路由到文档的 mp4 文件请求)
感谢您的帮助!
编辑 1:
想法 1 是不可能的,因为 Bundle 目录是只读的。
无法在 Web_assets 中对文档进行符号链接。
我也无法将 Web_assets 符号链接到文档中(奇怪的错误)。
所以我现在决定在每次启动时将 Web_assets 复制到 Document 中,这样我的 Document 文件夹就可以作为我的视频和 Web 资产的 baseURL。
这有点脏,因为我的所有 Web 资产现在都在我的文档文件夹中可见(但不可修改,因为该文件夹已被删除并从应用程序重新启动时从 Bundle 复制)
我最终以 EDIT 1 结束,@ngbaanh 建议:
在 viewDidLoad 上:
let filemgr = FileManager.default
docURL = filemgr.urls(for: .documentDirectory, in: .userDomainMask)[0]
destPath = docURL.path+"/www/"
sourcePath = Bundle.main.resourceURL!.appendingPathComponent("Web_Assets").path
//COPY Web_Assets content from Bundle to Documents/www
do {
try filemgr.removeItem(atPath: destPath)
} catch {
print("Error: \(error.localizedDescription)")
}
do {
try filemgr.copyItem(atPath: sourcePath, toPath: destPath)
} catch {
print("Error: \(error.localizedDescription)")
}
// Configure Webview
webView.navigationDelegate = self
webView.configuration.userContentController.add(self, name: "teleco")
do {
let baseURL = try filemgr.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
let fileURL = baseURL.appendingPathComponent("www/index.html")
self.loadFileURL(fileURL, allowingReadAccessTo: baseURL)
} catch {
print(error)
}
这样我的baseURL是Documents/www/
所以在我的 index.html 中,我可以包含像 style.css
这样的资源
而且我还可以使用 ../video.mp4
等相对路径访问 Documents/ 中的文件
所以 index.html 和 style.css 是通过 Bundle 分发的,在运行时复制到 Documents,这样我也可以访问 Documents 文件。
有点扭曲,但效果很好。
缺点:它会暴露用户可访问的文档文件夹中的 Web_assets 个文件。
但它们不能更改,因为它们在应用程序启动时被替换。
我是 运行 一个使用 WKWebView 的网络应用程序,它从名为 [=31= 的 Bundle 目录加载内容和资产(html、js、img、css...) ]Web_Assets。
我这样加载 index.html,并将 Web_assets 设置为 baseURL:
if let filePath = Bundle.main.path(forResource:"index", ofType:"html", inDirectory: "Web_Assets") {
let html = try String(contentsOfFile: filePath, encoding: .utf8)
webView.loadHTMLString(html, baseURL: Bundle.main.resourceURL?.appendingPathComponent("Web_Assets"))
}
这完美地工作:index.html 被加载并且 Web_Assets 作为其他资源的基本路径没有任何问题。 我也可以使用等效的 loadFileURL 方法(根据我的理解)。
但是,现在我想在我的 WKWebView 中使用来自 Document 目录的其他资源(在我的例子中是视频),该目录是静态的并且可以通过 iTunes 文件共享进行管理,而当然保持当前架构不变(Bundle 目录中的 Web 资产)。
例如,我的页面 index.html 会从 Web_Assets 加载他的 style.css 和其他资产并显示 HTML5 视频位于 文档
可以这样做吗?
想法(我不知道如何实现):
- Web_assets 中文档的符号链接?
- WKWebView 中不同文件类型的不同 baseURL? (将所有请求路由到 Web_Assets 除了路由到文档的 mp4 文件请求)
感谢您的帮助!
编辑 1: 想法 1 是不可能的,因为 Bundle 目录是只读的。 无法在 Web_assets 中对文档进行符号链接。 我也无法将 Web_assets 符号链接到文档中(奇怪的错误)。 所以我现在决定在每次启动时将 Web_assets 复制到 Document 中,这样我的 Document 文件夹就可以作为我的视频和 Web 资产的 baseURL。
这有点脏,因为我的所有 Web 资产现在都在我的文档文件夹中可见(但不可修改,因为该文件夹已被删除并从应用程序重新启动时从 Bundle 复制)
我最终以 EDIT 1 结束,@ngbaanh 建议:
在 viewDidLoad 上:
let filemgr = FileManager.default docURL = filemgr.urls(for: .documentDirectory, in: .userDomainMask)[0] destPath = docURL.path+"/www/" sourcePath = Bundle.main.resourceURL!.appendingPathComponent("Web_Assets").path //COPY Web_Assets content from Bundle to Documents/www do { try filemgr.removeItem(atPath: destPath) } catch { print("Error: \(error.localizedDescription)") } do { try filemgr.copyItem(atPath: sourcePath, toPath: destPath) } catch { print("Error: \(error.localizedDescription)") } // Configure Webview webView.navigationDelegate = self webView.configuration.userContentController.add(self, name: "teleco") do { let baseURL = try filemgr.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false) let fileURL = baseURL.appendingPathComponent("www/index.html") self.loadFileURL(fileURL, allowingReadAccessTo: baseURL) } catch { print(error) }
这样我的baseURL是Documents/www/ 所以在我的 index.html 中,我可以包含像 style.css
这样的资源而且我还可以使用 ../video.mp4
等相对路径访问 Documents/ 中的文件所以 index.html 和 style.css 是通过 Bundle 分发的,在运行时复制到 Documents,这样我也可以访问 Documents 文件。
有点扭曲,但效果很好。
缺点:它会暴露用户可访问的文档文件夹中的 Web_assets 个文件。 但它们不能更改,因为它们在应用程序启动时被替换。