This line failed because there was no entry for the parent directory.
|
try Data().write(to: fileURL) |
Adding this code before made it work:
let parentDirPath = fileURL.path.split(separator: "/").dropLast().joined(separator: "/")
if !FileManager.default.fileExists(atPath: parentDirPath) {
try FileManager.default.createDirectory(atPath: parentDirPath, withIntermediateDirectories: true)
}