iOS:WKWebview 如何允许位于 bundle 中的 html 文件对文档目录进行读取访问 [英] iOS: WKWebview how to allow html file located in bundle read access to documents directory

查看:18
本文介绍了iOS:WKWebview 如何允许位于 bundle 中的 html 文件对文档目录进行读取访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 html 文件位于一个包中,我正在使用 WKWebView 从这个 html 加载内容:

I have an html file located in a bundle and I'm using WKWebView to load content from this html:

let fileURL = URL(fileURLWithPath: Bundle.main.path(forResource:"demo", ofType: "html")!)
    webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)

然而,这个 html 文件有一些脚本试图访问位于 Documents 目录中的文件(一些保存在那里的视频),但得到无法加载本地资源".错误.我在 AVPlayerViewController 的其他地方使用了这个文件,所以我知道这些文件存在并且我使用的 url 是正确的.

However, this html file have some scripts which are trying to get access to files located in Documents dir (some videos saved in there) but get a "can't load local resources." error. I'm using this files somewhere else in AVPlayerViewController so I know the files exists and the url I'm using is the right one.

我试过了:

1. 将文档目录 url 传递给 allowedReadAccessTo 参数 - webview 甚至不加载

1. pass the documents dir url to the allowingReadAccessTo parameter - the webview doesn't even load

2. 将项目从文档目录复制到 tmp 目录并在脚本中传递这个 (tmp dir) url - 它有效,但是这个过程发生的太多并且由于内存问题和大文件需要的时间,我不想复制这么多文件.

2. Copy the items from documents dir to tmp dir and passing this (tmp dir) url in the scripts - it works, but this process happens too much and I don't want to copy so many files due to memory issues and the time it take for big files.

3. 将 html 文件及其脚本从包复制到文档目录 - 不相关,因为我使用的脚本会不时发生变化,所以要确保用户始终拥有(脚本的)最后一个版本,每次应用启动时我都必须复制它.

3. Copy the html file and its scripts from the bundle to documents dir - not relevant because the scripts Im using changes from time to time, so to make sure the user always have the last version (of the scripts) I will have to copy it every time the app launches.

那么,有没有其他方法可以让 html 文件(位于 bundle 中)访问文档文件?

So, is there any other way to allow html file (located in bundle) access the documents file?

提前致谢,

推荐答案

这涵盖了一个文件,但您应该能够使用以下代码构建一个将相关文件复制到文档目录的函数.

This covers one file, but you should be able to build a function that copies the relevant files to the Documents Directory using the code below.

///Copy the file from the bundle to the Documents Directory
guard let bundleURL = Bundle.main.url(forResource:"demo", ofType: "html") else {
     preconditionFailure("Unable to get bundle URL") 
}

guard let documentsDirectory = let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
     preconditionFailure("Unable to get Documents URL") 
}

let newURL = documentsDirectory.appendingPathComponent("demo.html")

do {
   try FileManager.default.copyItem(at: bundleURL, to: newUrl)
} catch {
   print("Error copying bundle: \(error)")
}


///Setup the WKWebView
let prefs = WKPreferences()
prefs.javaScriptEnabled = true

let webConfig = WKWebViewConfiguration()
webConfig.preferences = prefs

var webViewer = WKWebView(frame: view.frame, configuration: webConfig)
webViewer.loadFileURL(newURL, allowingReadAccessTo: documentsDirectory)

请记住,Bundle 完全位于您的应用程序内的不同目录中.仅仅因为文件可以访问文档目录并不意味着文件中的 HTML 将能够找到您正在引用的引用脚本或其他 HTML 文件.您应该考虑捆绑包中的所有内容都是不可变的,并且位置本身只是一个源.

Keep in mind, the Bundle is located in a different directory altogether within your app. Just because the file has access to the Documents Directory does not mean that the HTML within the file will be able to find the referencing scripts or other HTML files you're referencing. You should consider everything within the bundle to be immutable and the location itself a source origin only.

这篇关于iOS:WKWebview 如何允许位于 bundle 中的 html 文件对文档目录进行读取访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆