简体   繁体   English

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

[英]Bundle.main.url(forResource: “hello”, withExtension: “html”) is returning nil

I have created hello.html file .when I am try to access it,url is returning nil everytime. 我创建了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. drop hello.html in your project 将hello.html放到您的项目中
  2. click Copy items if needed 如果需要,请单击复制项目
  3. add to targets project 添加到目标项目
  4. it's ok 没关系

暂无
暂无

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

相关问题 Bundle.main.url(forResource: "AppleIncRootCertificate", withExtension: "cer") == nil - Bundle.main.url(forResource: "AppleIncRootCertificate", withExtension: "cer") == nil Bundle.main.path(forResource :)返回nil - Bundle.main.path(forResource:) returns nil Bundle.main.url 找不到文件 - Bundle.main.url cannot find file URL(fileURLWithPath:Bundle.main.path(forResource:“ ports”,ofType:“ geojson”)!)是零吗? - URL(fileURLWithPath: Bundle.main.path(forResource: “ports”, ofType: “geojson”)!) is nil? Bundle.main.path(forResource:ofType:inDirectory:) 返回 nil - Bundle.main.path(forResource:ofType:inDirectory:) returns nil Bundle.main.url 无法找到使用 PropertyListSerialization 保存的 plist - Bundle.main.url is unable to locate plist saved with PropertyListSerialization 核心数据:bundle.main.url 线程 1 致命错误 - Core Data: bundle.main.url Thread 1 fatal error 当目录和文件名正确时,Bundle.main.path(forResource:ofType:inDirectory :)返回nil - Bundle.main.path(forResource:ofType:inDirectory:) returns nil when directory and filename are correct 当应用名称包含“空格”时,Bundle.main.path(forResource:ofType:inDirectory :)返回nil - Bundle.main.path(forResource:ofType:inDirectory:) returns nil when app name contains “space” 为什么Bundle.main.path(forResource:fileName,ofType:“ txt”)总是返回nil? - why does Bundle.main.path(forResource: fileName, ofType: “txt”) always return nil?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM