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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:55:48  来源:igfitidea点击:

What's the difference between UIWebView and WKWebView when loading local resources

iosuiwebviewwkwebview

提问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:

几点:

  1. Apple recommendsthat you use WKWebviewfor iOS 8 and later. I would avoid writing new code with UIWebView.
  1. Apple建议您使用WKWebviewiOS 8 及更高版本。我会避免使用UIWebView.

In apps that run in iOS 8 and later, use the WKWebViewclass instead of using UIWebView. Additionally, consider setting the WKPreferencesproperty javaScriptEnabledto falseif you render files that are not supposed to run JavaScript.

在 iOS 8 及更高版本中运行的应用程序中,使用WKWebView类而不是使用UIWebView. 此外,考虑设置WKPreferences属性javaScriptEnabledfalse,如果你渲染不应该运行JavaScript文件。

  1. 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.pngand use file:///path/to/file.pnginstead.
  1. Apple 一直试图摆脱路径,而是希望甚至对本地文件使用URI。他们建议您不要使用/path/to/file.pngfile:///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
  • urldoes 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. UIWebViewsupports it for backwards-compatibility reasons but Apple decided to start clean with WKWebView.
  • fileURLhas a scheme (file://) that tells the resource is located on the local file system. Other common schemes are http, 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://) 告诉资源位于本地文件系统上。其他常见方案是httphttpsftp等。它是资源的完整地址,因此两个视图都知道如何解决它。

回答by Ryan H.

This might be for security reasons, or just how the WKWebViewAPI was implemented.

这可能是出于安全原因,或者只是WKWebViewAPI 是如何实现的。

WKWebViewhas 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 WKWebViewinstead of UIWebView. See: https://developer.apple.com/reference/webkit/wkwebview

如果你的目标的iOS 8.0或更高版本,你应该使用WKWebView代替UIWebView。请参阅:https: //developer.apple.com/reference/webkit/wkwebview