问题描述
我想用 webView 加载本地资源.我用 UIWebView 和 WKWebView 构建了一个演示,用下面的代码做一些测试.
let uiWebView = UIWebView(frame: self.view.bounds)self.view.addSubview(uiWebView)让 wkWebView = WKWebView(frame:CGRect(x: 0, y: 400, width: 500, height: 500))self.view.addSubview(wkWebView)let path = Bundle.main.path(forResource:"1", ofType: "png")守卫让 realPath = path else {返回}让 url = URL(string: realPath)让 fileUrl = URL(fileURLWithPath: realPath)如果让 realUrl = url {uiWebView.loadRequest(URLRequest(url:realUrl))wkWebView.load(URLRequest(url:realUrl))}//uiWebView.loadRequest(URLRequest(url:fileUrl))//wkWebView.load(URLRequest(url:fileUrl))
uiWebView 可以加载资源但 wkWebView 不能.但是如果我使用
uiWebView.loadRequest(URLRequest(url:fileUrl))wkWebView.load(URLRequest(url:fileUrl))
uiWebView 和 wkWebView 都可以很好地工作.我很困惑,谁能为我解释一下:我不应该对本地资源使用 URL(string: realPath) 吗?但是为什么 UIWebView 可以使用呢?
几点:
- Apple 建议您在 iOS 8 中使用
WKWebview
然后.我会避免使用UIWebView
编写新代码.
在 iOS 8 及更高版本中运行的应用程序中,使用 WKWebView
类而不是使用 UIWebView
.此外,如果您呈现不应运行 JavaScript 的文件,请考虑将 WKPreferences
属性 javaScriptEnabled
设置为 false
.
- Apple 一直试图摆脱 path,而是希望使用 URI 甚至本地文件.他们建议您不要使用
/path/to/file.png
而是使用file:///path/to/file.png
.
至于为什么一个 URL 有效而另一个无效,让我们举一个最小的例子:
let realPath = "/path/to/file.png"让 url = URL(string: realPath)///path/to/file.png让 fileUrl = URL(fileURLWithPath: realPath)//file:///path/to/file.png
url
不提供方案(又名协议).它只能与另一个 URL 结合使用,以提供您尝试访问的资源的绝对地址.UIWebView
出于向后兼容的原因支持它,但 Apple 决定从WKWebView
开始.fileURL
有一个方案 (file://
),它告诉资源位于本地文件系统上.其他常见方案有http
、https
、ftp
等.它是资源的完整地址,因此两个视图都知道如何解析它.
I want to load local resources with webView. I built a demo with both UIWebView and WKWebView to do some test with the code below.
let uiWebView = UIWebView(frame: self.view.bounds)
self.view.addSubview(uiWebView)
let wkWebView = WKWebView(frame:CGRect(x: 0, y: 400, width: 500, height: 500))
self.view.addSubview(wkWebView)
let path = Bundle.main.path(forResource:"1", ofType: "png")
guard let realPath = path else {
return
}
let url = URL(string: realPath)
let fileUrl = URL(fileURLWithPath: realPath)
if let realUrl = url {
uiWebView.loadRequest(URLRequest(url:realUrl))
wkWebView.load(URLRequest(url:realUrl))
}
// uiWebView.loadRequest(URLRequest(url:fileUrl))
// wkWebView.load(URLRequest(url:fileUrl))
The uiWebView can load the resource but wkWebView can not. But if I use
uiWebView.loadRequest(URLRequest(url:fileUrl))
wkWebView.load(URLRequest(url:fileUrl))
both uiWebView and wkWebView can work well.I am confused and can anyone explain that for me:Shouldn't I use URL(string: realPath) for a local resource? But why UIWebView can use it ?
A couple points:
- Apple recommends that you use
WKWebview
for iOS 8 and later. I would avoid writing new code withUIWebView
.
- Apple has been trying to move away from path and instead wants to use URI even for local files. They recommend that you NOT use
/path/to/file.png
and usefile:///path/to/file.png
instead.
As to why one URL works and the other does not, let's make a minimal example:
let realPath = "/path/to/file.png"
let url = URL(string: realPath) // /path/to/file.png
let fileUrl = URL(fileURLWithPath: realPath) // file:///path/to/file.png
url
does not provide the scheme (a.k.a protocol). It should only be used in conjunction with another URL to give the absolute address of the resource you are trying to reach.UIWebView
supports it for backwards-compatibility reasons but Apple decided to start clean withWKWebView
.fileURL
has a scheme (file://
) that tells the resource is located on the local file system. Other common schemes arehttp
,https
,ftp
, etc. It's a complete address to a resource so both views know how to resolve it.
这篇关于加载本地资源时 UIWebView 和 WKWebView 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!