首页 > 解决方案 > 下载后如何在 WKWebView 中加载本地文件(不在捆绑包中),并在 url 中使用参数?

问题描述

我试图下载文件,并将它们加载到 WKWebView 中。如果我用 .html 文件加载它们就可以了,但是当我想指示一个参数直接转到特定网页时,它不会加载页面。它给了我一个白页。

如何将参数放在 url 中并将其加载到 WKWebView 中?

    let url = Constants.Path.document + "/APP/index.html?180&2"
    let nsurl = URL(fileURLWithPath: url) //locally
    let readAccessToURL = nsurl.deletingLastPathComponent()
    webView?.loadFileURL(nsurl, allowingReadAccessTo: readAccessToURL)

(如果我只用 index.html 加载它,它可以工作,如果我使用具有相同 url 和参数的 UIWebView,它可以工作)

提前致谢

标签: iosswifturlwkwebview

解决方案


您可以尝试使用#而不是?在准备时使用url吗?还有一件事-我可以看到您的资源位于您的设备上,您可能需要"file://"在完整路径之前添加前缀。之后,我能够加载预先选择的锚点。我尝试使用html我的文件Bundle.main

class ViewController: UIViewController {

    var webView: WKWebView?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let weburl = "file://" + Bundle.main.path(forResource: "WebContent", ofType: "html")! + "#Lorem_Ipsum4"
        
        // Web view
        webView = WKWebView()
        webView!.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(webView!)
        
        webView!.widthAnchor.constraint(equalTo: view.widthAnchor, constant: 0.0).isActive = true
        webView!.heightAnchor.constraint(equalTo: view.heightAnchor, constant: 0.0).isActive = true
        webView!.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0).isActive = true
        webView!.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0).isActive = true
        webView?.load(URLRequest(url: URL(string: weburl)!))
    }
}

这是我的WebContent.html

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
    .main-content {
      height: 100vh;
      text-align: justify;
    }
    </style>
  </head>
  <body>
    <h2 id="Lorem_Ipsum1">Lorem Ipsum</h2>
    <p class="main-content">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
    </p>
    <h2 id="Lorem_Ipsum2">Lorem Ipsum</h2>
    <p class="main-content">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
    </p>
    <h2 id="Lorem_Ipsum3">Lorem Ipsum</h2>
    <p class="main-content">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
    </p>
    <h2 id="Lorem_Ipsum4">Lorem Ipsum</h2>
    <p class="main-content">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
    </p>
    <p>Go to the
      <a href="#Lorem_Ipsum1">top</a>.
    </p>
  </body>
</html>


推荐阅读