简体   繁体   English

加载本地资源时,UIWebView和WKWebView之间有什么区别

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

I want to load local resources with webView. 我想用webView加载本地资源。 I built a demo with both UIWebView and WKWebView to do some test with the code below. 我用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. uiWebView可以加载资源,但wkWebView不能。 But if I use 但是,如果我使用

  uiWebView.loadRequest(URLRequest(url:fileUrl))
  wkWebView.load(URLRequest(url:fileUrl))

both uiWebView and wkWebView can work well. uiWebView和wkWebView都能很好地工作。 I am confused and can anyone explain that for me: Shouldn't I use URL(string: realPath) for a local resource? 我很困惑,任何人都可以解释一下:我不应该为本地资源使用URL(字符串:realPath)吗? But why UIWebView can use it ? 但是为什么UIWebView可以使用它呢?

A couple points: 几点:

  1. Apple recommends that you use WKWebview for iOS 8 and later. Apple 建议您在iOS 8及更高版本中使用WKWebview I would avoid writing new code with UIWebView . 我会避免用UIWebView编写新代码。

In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView . 在iOS 8及更高版本中运行的应用程序中,使用WKWebView类而不是使用UIWebView Additionally, consider setting the WKPreferences property javaScriptEnabled to false if you render files that are not supposed to run JavaScript. 此外,如果渲染不应运行JavaScript的文件,请考虑将WKPreferences属性javaScriptEnabled设置为false

  1. Apple has been trying to move away from path and instead wants to use URI even for local files. Apple一直试图摆脱路径 ,而是希望将URI用于本地文件。 They recommend that you NOT use /path/to/file.png and use file:///path/to/file.png instead. 他们建议您不要使用/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 (aka protocol). url不提供方案(aka协议)。 It should only be used in conjunction with another URL to give the absolute address of the resource you are trying to reach. 它应该只与另一个URL一起使用,以提供您尝试访问的资源的绝对地址。 UIWebView supports it for backwards-compatibility reasons but Apple decided to start clean with WKWebView . UIWebView支持它是出于向后兼容的原因,但Apple决定用WKWebView开始清理。
  • fileURL has a scheme ( file:// ) that tells the resource is located on the local file system. fileURL有一个方案( file:// ),告诉资源位于本地文件系统上。 Other common schemes are http , https , ftp , etc. It's a complete address to a resource so both views know how to resolve it. 其他常见的方案是httphttpsftp等。它是资源的完整地址,因此两个视图都知道如何解决它。

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:) . WKWebView有一个特定的实例方法,用于加载名为loadFileURL(_:allowingReadAccessTo:) WKWebView loadFileURL(_:allowingReadAccessTo:)本地资源。 This was introduced in iOS 9. 这是在iOS 9中引入的。

Note 注意

If you are targeting iOS 8.0 or newer, you should be using WKWebView instead of UIWebView . 如果您的目标是iOS 8.0或更高版本,则应该使用WKWebView而不是UIWebView See: https://developer.apple.com/reference/webkit/wkwebview 请参阅: https//developer.apple.com/reference/webkit/wkwebview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM