【问题标题】:how to load local html file to string variable in ios swift?如何在ios swift中将本地html文件加载到字符串变量?
【发布时间】:2018-04-30 04:46:35
【问题描述】:

我想将我的本地 html 文件加载到字符串变量中。我不知道该怎么做。请帮我。 我在下面找到了链接,但它是从在线 url 加载的。 Swift & UIWebView element to hide

【问题讨论】:

    标签: html ios string swift uiwebview


    【解决方案1】:

    将 html 复制到您的项目目录并使用此代码:

    @IBOutlet weak var webView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
    
            var htmlFile = NSBundle.mainBundle().pathForResource("MyHtmlFile", ofType: "html")
            var htmlString = try? String(contentsOfFile: htmlFile!, encoding: NSUTF8StringEncoding)
            webView.loadHTMLString(htmlString!, baseURL: nil)
    
    
    }
    

    【讨论】:

    • 我很困惑。确切的路径在哪里定义?我是 swift 和 xcode 的新手:/ 但是我不知道......也许:如何在 xcode 项目中加载文件以这种方式定位它?
    • @Tomas Javurek 只需将您的文件拖到项目文件夹中
    • @RizwanShaikh 我是这样做的,很直观。我的 xcode 项目中已经有“default.html”,我尝试以这种方式加载它let htmlFile = Bundle.main.path(forResource: "default", ofType: "html")。该文件也是编译包的一部分,位于 Resources 文件夹中。但是当我尝试获取文件的内容时,它会产生错误。我还尝试打印 htmlFile 的值,展开后,它返回 nil...,所以我的问题出在其他地方...知道我做错了什么吗?
    • 我用这个代码弄明白了:let bundle = Bundle(for: MyView.self) let url = bundle.url(forResource:"default", withExtension:"html") let request = URLRequest(url: url!) self.webView.load(request)
    • @TomasJavurek,你是我的英雄。您应该将其添加为答案。
    【解决方案2】:

    在 Swift 3 中:

        let htmlFile = Bundle.main.path(forResource:"MyHtmlFile", ofType: "html")
        let htmlString = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
        webView.loadHTMLString(htmlString!, baseURL: nil)
    

    我建议使用optional chaining,而不是像上面那样强制解开htmlString。

    【讨论】:

      【解决方案3】:

      您可以编写一些这样的实用方法并获取 html 字符串并将其加载到 webview。我在这里使用了 URL 方法

         private func getHTML() -> String {
              var html = ""
              if let htmlPathURL = Bundle.main.url(forResource: "test", withExtension: "html"){
                  do {
                      html = try String(contentsOf: htmlPathURL, encoding: .utf8)
                  } catch  {
                      print("Unable to get the file.")
                  }
              }
      
              return html
          }
      

      【讨论】:

        【解决方案4】:

        Swift 3 语法:

            guard
                let file = Bundle.main.path(forResource: "agreement", ofType: "html"), 
                let html = try? String(contentsOfFile: file, encoding: String.Encoding.utf8)
            else {
                return
            }
            webView.loadHTMLString(html, baseURL: nil)
        

        【讨论】:

          【解决方案5】:

          简单回答

           let indexPath = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "/")
                  if let indexPath = indexPath
                  {
                      do
                      {
                          let htmlContent = try String(contentsOfFile: indexPath, encoding: String.Encoding.utf8)
          
                          let base = Bundle.main.resourceURL 
                          self.webView.loadHTMLString(htmlContent, baseURL: base)
          
                      }
                      catch let err as NSError
                      {
                          print(err.debugDescription)
                      }
                  }
          

          【讨论】:

            猜你喜欢
            • 2010-12-24
            • 1970-01-01
            • 1970-01-01
            • 2021-04-26
            • 2015-08-20
            • 1970-01-01
            • 2016-11-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多