如何将捆绑包 html 文件复制到 iOS Swift 中的文档目录?

How to copy a bundle html file to a document directory in iOS Swift?

我一直在尝试将本地 index.html 文件保存到文档目录,但它没有保存到本地文档目录。

这是我的文件夹结构:

sampleHtml.xcodeproj
---->sampleHtml
-------->Web_Assets
------------> index.html
---->ViewController.swift

这里是保存和检查文件路径是否存在的代码,如果路径存在则覆盖它,否则复制新的。

func saveHtmlDoc(){
    let filemgr = FileManager.default
    let docURL = filemgr.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let destPath = docURL.path+"/www/"
    print("des path", destPath)
    let sourcePath = Bundle.main.resourceURL!.appendingPathComponent("Web_Assets").path
    print("sourc", sourcePath.appending("/index.html"))

      //COPY Web_Assets content from Bundle to Documents/www
      do {
          try filemgr.removeItem(atPath: destPath)
      } catch {
          print("Error: \(error.localizedDescription)")
      }
      do {
          try filemgr.copyItem(atPath:  sourcePath + "/", toPath: destPath)
      } catch {
          print("Error: \(error.localizedDescription)")
      }

}

我的问题是如何将 index.html 文件保存到文档目录并检查文件路径是否存在,如果存在则覆盖它或复制新路径。

非常感谢任何帮助...

我刚刚在一个有效的测试项目中完成了这段代码。

您应该确保一路执行检查并确保您的 HTML 文件处于复制资源构建阶段

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        moveHtmlFile()
    }

    private func moveHtmlFile() {
        let fileManager = FileManager.default
        let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
        guard let sourcePath = Bundle.main.path(forResource: "index", ofType: "html") else {
            return
        }

        if fileManager.fileExists(atPath: sourcePath) {
            let sourceUrl = URL(fileURLWithPath: sourcePath)
            try? fileManager.createDirectory(atPath: documentsDirectory.appendingPathComponent("www").path,
                                             withIntermediateDirectories: false,
                                             attributes: nil)
            let destination = documentsDirectory.appendingPathComponent("www/index.html", isDirectory: false)
            try? fileManager.copyItem(at: sourceUrl, to: destination)

            if fileManager.fileExists(atPath: destination.path) {
                print("file copied")
            } else {
                print("file copy failed")
            }
        }
    }

}

结果:

我尝试测试下面的代码,它工作正常。在添加文件之前,您需要先创建文件夹。

func saveHtmlDoc() {
    let filemgr = FileManager.default
    let docURL = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    let destPath = URL(string:docURL)?.appendingPathComponent("www")

    guard let newDestPath = destPath, let sourcePath = Bundle.main.path(forResource: "image", ofType: ".png"), let fullDestPath = NSURL(fileURLWithPath: newDestPath.absoluteString).appendingPathComponent("image.png") else { return  }

    //CREATE FOLDER
    if !filemgr.fileExists(atPath: newDestPath.path) {
        do {
            try filemgr.createDirectory(atPath: newDestPath.path, withIntermediateDirectories: true, attributes: nil)
        } catch {
            print(error.localizedDescription);
        }
    }
    else {
        print("Folder is exist")
    }

    if filemgr.fileExists(atPath: fullDestPath.path) {
        print("File is exist in \(fullDestPath.path)")
    }
    else {
        do {
            try filemgr.copyItem(atPath:  sourcePath, toPath: fullDestPath.path)
        } catch {
            print("Error: \(error.localizedDescription)")
        }
    }
}