Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Bundle.main.url(forResource:“hello”,扩展名为“html”)返回nil_Ios_Swift - Fatal编程技术网

Ios Bundle.main.url(forResource:“hello”,扩展名为“html”)返回nil

Ios Bundle.main.url(forResource:“hello”,扩展名为“html”)返回nil,ios,swift,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){

我已经创建了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.
    }

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

  • 您的HTML文件未添加到目标中。明白了,谢谢男士:)您的HTML文件未添加到目标中。明白了,谢谢男士:)但HTML文件已在项目中。此答案仅在向项目中添加新文件时有用。如果在sanbox中添加html文件,请使用
    FileManager
    get file path,然后webview加载,可以,但html文件已在项目中。此答案仅在向项目中添加新文件时有用。如果在sanbox中添加html文件,请使用
    FileManager
    get file path,然后使用webview加载即可