I am working on a file download currently. After the download is finished I want to move the downloaded file to the apps document directory, my setup:
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
debugPrint("didFinishDownloadingTo: \(location)")
let documentsDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let saveDirectory = documentsDirectory.appendingPathComponent("file1.mp4")
do {
try FileManager.default.moveItem(at: location, to: saveDirectory)
} catch {
print("didFinishDownloadingTo error \(error.localizedDescription)")
}
}
At some point I want to see all my downloaded files:
do {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = String(describing: paths[0])
let items = try FileManager.default.contentsOfDirectory(atPath: documentsDirectory)
for item in items {
print("Found \(item)")
}
} catch {
print("contentsOfDirectory \(error.localizedDescription)")
}
This part throws:
The folder “Documents” doesn’t exist.
The didFinishDownloadingTo method throws no error, so I assume the moveitem function succeeded, but I have no idea why I cant list my items... I did some research, but with no result and I dont know why I can't list.