Search code examples
iosswiftxcodewkwebviewnsfilemanager

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


I have been trying to save local index.html file to document directory but it does not save to local document directory.

here is my folder structure:

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

Here is the code for saving and checking file path exist is not, if path exist then overwrite it else copy the new one.

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)")
      }

}

My issue is how to save index.html file to document directory and check the file path exist are not, if exist overwrite it or copy the new one.

Any help much appreciates it...


Solution

  • I have just done this code in a test project which works.

    You should ensure that you are performing checks along the way and ensure that your HTML file is in your Copy resources build phase

    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")
                }
            }
        }
    
    }
    

    Result:

    enter image description here