ios 加载本地资源时 UIWebView 和 WKWebView 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41134493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between UIWebView and WKWebView when loading local resources
提问by Rufus
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.
我想用 webView 加载本地资源。我用 UIWebView 和 WKWebView 构建了一个演示,用下面的代码做一些测试。
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 可以加载资源但 wkWebView 不能。但如果我使用
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 ?
uiWebView 和 wkWebView 都可以很好地工作。我很困惑,谁能为我解释一下:我不应该对本地资源使用 URL(string: realPath) 吗?但是为什么 UIWebView 可以使用呢?
回答by Code Different
A couple points:
几点:
- Apple recommendsthat you use
WKWebview
for iOS 8 and later. I would avoid writing new code withUIWebView
.
- Apple建议您使用
WKWebview
iOS 8 及更高版本。我会避免使用UIWebView
.
In apps that run in iOS 8 and later, use the
WKWebView
class instead of usingUIWebView
. Additionally, consider setting theWKPreferences
propertyjavaScriptEnabled
tofalse
if you render files that are not supposed to run JavaScript.
在 iOS 8 及更高版本中运行的应用程序中,使用
WKWebView
类而不是使用UIWebView
. 此外,考虑设置WKPreferences
属性javaScriptEnabled
来false
,如果你渲染不应该运行JavaScript文件。
- Apple has been trying to move away from pathand instead wants to use URIeven for local files. They recommend that you NOT use
/path/to/file.png
and usefile:///path/to/file.png
instead.
- Apple 一直试图摆脱路径,而是希望甚至对本地文件使用URI。他们建议您不要使用
/path/to/file.png
,file:///path/to/file.png
而是使用。
As to why one URL works and the other does not, let's make a minimal example:
至于为什么一个 URL 有效而另一个无效,让我们举一个最小的例子:
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.
url
不提供方案(又名协议)。它只能与另一个 URL 结合使用,以提供您尝试访问的资源的绝对地址。UIWebView
出于向后兼容的原因支持它,但 Apple 决定从WKWebView
.fileURL
有一个方案 (file://
) 告诉资源位于本地文件系统上。其他常见方案是http
、https
、ftp
等。它是资源的完整地址,因此两个视图都知道如何解决它。
回答by Ryan H.
This might be for security reasons, or just how the WKWebView
API was implemented.
这可能是出于安全原因,或者只是WKWebView
API 是如何实现的。
WKWebView
has a specific instance method for loading local resources called loadFileURL(_:allowingReadAccessTo:)
. This was introduced in iOS 9.
WKWebView
有一个用于加载本地资源的特定实例方法,称为loadFileURL(_:allowingReadAccessTo:)
. 这是在 iOS 9 中引入的。
Note
笔记
If you are targeting iOS 8.0 or newer, you should be using WKWebView
instead of UIWebView
. See: https://developer.apple.com/reference/webkit/wkwebview
如果你的目标的iOS 8.0或更高版本,你应该使用WKWebView
代替UIWebView
。请参阅:https: //developer.apple.com/reference/webkit/wkwebview