Hi. I've been struggling with this for quite sometime, and none of the solutions here or on StackOverflow work for me. In the app's Documents directory I create an images folder, and inside it there are many other folders and each one contains images. I do have permissions to create and read each folder and image, but I can't delete them. I get the following error:
Error Domain=NSCocoaErrorDomain Code=513 "“Front” couldn’t be removed because you don’t have permission to access it." UserInfo={NSUserStringVariant=(Remove), NSFilePath=/var/mobile/Containers/Data/Application/643B50DD-8696-4C5D-9EC2-106AFE317B8B/Documents/horseImages/54da3ac3-bdbc-4670-a848-b507abf5fcc0/Front, NSUnderlyingError=0x280e12a00 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
I create the folders like this:
try fileManager.createDirectory(at: imagesUrl, withIntermediateDirectories: true, attributes: [:])
but I also tried setting the attributes as nil, and also giving the .posixPermissions key a 777 value.
The way I try to remove is with:
try fileManager.removeItem(at: imagesFolderURL)
having tried both with URL and with the path. I tried deleting the entire images folder as well as each image individually.
Also, I tried using a bookmark inside the horseImages directory, but it also does not work:
try fileManager.createDirectory(at: imagesUrl, withIntermediateDirectories: true, attributes: [:])
} catch {
print("Error, maybe in attrubutes: \(error)")
}
do {
// Start accessing a security-scoped resource.
guard imagesUrl.startAccessingSecurityScopedResource() else {
// Handle the failure here.
return
}
// Make sure you release the security-scoped resource when you finish.
defer { imagesUrl.stopAccessingSecurityScopedResource() }
let bookmarkData = try imagesUrl.bookmarkData(options: .minimalBookmark, includingResourceValuesForKeys: nil, relativeTo: nil)
try bookmarkData.write(to: imagesUrl.appendingPathComponent("bookmark"))
}
catch let error {
print("Error creating bookmark, \(error)")
}
and reading it like:
guard let url = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let imagesFolderURL = url.appendingPathComponent("horseImages")
do {
let bookmarkData = try Data(contentsOf: imagesFolderURL.appendingPathComponent("bookmark"))
var isStale = false
let _ = try URL(resolvingBookmarkData: bookmarkData, bookmarkDataIsStale: &isStale)
guard !isStale else {
// Handle stale data here.
return
}
if fileManager.fileExists(atPath: imagesFolderURL.path) {
do {
try fileManager.removeItem(at: imagesFolderURL)
print("Images deleted from device")
} catch {
print("Images were not deleted from device, bacause: \(error)")
}
}
}
catch let error {
print("Error reading bookmark, \(error)")
}
The last thing I tried (as seen on other posts) was to turn off the App Sanbox, but the information I found was outdated as the ways of disabling it are not working on Xcode 14 (in the target capabilities there is not an option to disable it and the .entitlements file does not exist). Though, for what I understood, the Sandbox is for accessing files outside the app's range, and my images folder is inside the Documents directory.
Sorry for writing too much but I tried to give as much details as possible.
Thank you beforehand, I really hope someone can help me, it has been a true pain in the *** and this functionality is crucial for my app.