Error Deleting Contents in Directory - Domain=Nscocoaerrordomain Code=4 | Domain=Nsposixerrordomain Code=2 "No Such File or Directory"

unable to delete file from the app documents folder

Do not use colons in file names.

(For legacy reasons) the OS treats them as path separators and replaces them with slashes.

How to reproduce UIDocumentUIPickerViewController NSCocoaErrorDomain Code=260?

As Thomas Deniau suggested in the comments, we managed to reproduce the bug by saving it to files. Check this answer.

FileManager returns url with Nil instead of file on it

Found apple documentation about getting access to directories. Edited my code to this and now it is working

guard urls.first!.startAccessingSecurityScopedResource() else {
print("Error getting access")
return
}

defer { urls.first!.stopAccessingSecurityScopedResource() }

let path = urls.first!.path
let stream = InputStream(url: URL(fileURLWithPath: path))

do {
let csv = try CSVReader(stream: stream!)
print("Everything is ok")
while let row = csv.next() {
print(row)
}
URL(fileURLWithPath: path).stopAccessingSecurityScopedResource()
} catch {
print(error)
}

Basically before working with the URL you get the request to use secured URL by this function

guard urls.first!.startAccessingSecurityScopedResource() else {
print("Error getting access")
return
}

defer { urls.first!.stopAccessingSecurityScopedResource() }

And end working with secured connection by writing this line

URL(fileURLWithPath: path).stopAccessingSecurityScopedResource()

However code written in documentation is not working for me, and error check(that i deleted) still giving me an error

Update

If code is not working, you can delete this line

defer { urls.first!.stopAccessingSecurityScopedResource() }


Related Topics



Leave a reply



Submit