Bundle.main.url(forResource:“hello”,withExtension:“html”)返回nil

时间:2018-02-07 06:28:40

标签: ios swift

我创建了hello.html文件。当我尝试访问它时,url每次都返回nil。

  override func viewDidLoad() {
        super.viewDidLoad()
        if let url = Bundle.main.url(forResource: "hello", withExtension: "html"){
            if let htmlData = try? Data(contentsOf:url){
                let baseURL = URL(fileURLWithPath: Bundle.main.bundlePath)
                webView.load(htmlData, mimeType: "text/html", textEncodingName: "UTF-8", baseURL: baseURL)

            }
        }

        // Do any additional setup after loading the view.
    }

1 个答案:

答案 0 :(得分:1)

enter image description here

  1. 在项目中删除hello.html
  2. 如果需要,请单击“复制项目”
  3. 添加到目标项目
  4. 没关系