简体   繁体   English

对本地文件的引用无效

[英]reference to local file is invalid

This view is to display a local html file.该视图用于显示本地 html 文件。 When I launch it, the view never displays, the screen just hangs on the calling view.当我启动它时,视图永远不会显示,屏幕只是挂在调用视图上。 The exception is a nil value for let fileURL = .例外是let fileURL =的 nil 值。

import UIKit
import WebKit

class ViewController2: UIViewController, WKNavigationDelegate {
    var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let fileURL = URL(fileURLWithPath: Bundle.main.path(forResource:"about", ofType: "html")!)
        webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
    }
}

将 about.html 添加到项目中

You don´t need to cast the Bundle.main.path(forResource:"about", ofType: "html") to URL .您不需要将Bundle.main.path(forResource:"about", ofType: "html")URL Do it like this instead:改为这样做:

let fileURL = Bundle.main.url(forResource: "about", withExtension: "html")!
webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)

Update:更新:
Remember to also add the file in your Project settings > Your project target > Build Phases > Copy bundle resources otherwise it won´t work.请记住还要在您的项目设置>您的项目目标>构建阶段>复制包资源中添加该文件,否则它将无法工作。 When you add the file to your project remeber to:将文件添加到项目时,请记住:

  • Check "copy items if needed"勾选“需要时复制项目”
  • Select "create folder references"选择“创建文件夹引用”

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

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