Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One way to preserve the directory structure when uploading local files to the UIWebview on an iPhone is to use a file URL scheme.

First, get the local file path using a file manager:

let fileManager = FileManager.default
guard let filePath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("myFolder/myFile.pdf") else {
    return
}

Then, create a file URL with the path and load it into the webview:

let fileURL = URL(fileURLWithPath: filePath.absoluteString)
let request = URLRequest(url: fileURL)
webView.load(request)

This approach will preserve the directory structure of the file when loading it into the webview.