3

I use URL bookmark data (with security scope). After a file was moved to the trash (presumably when the App was not running), the bookmark is updated. That is generally good, but when a file was moved to the trash, I really want to reflect that and remove my bookmark.

The only way I can think to get around it is to store the original URL and compare their absoluteString or check for .Trash.

Is there a (simple?) way to check that bookmark data is now pointing to the Trash?

bauerMusic
  • 5,470
  • 5
  • 38
  • 53

1 Answers1

4

FileManager methods can be used if an URL refers to a file in a trash, this avoids to hard-code the trash folder path. Here is a translation of the Objective-C code in

to Swift, as an extension of URL:

extension URL {

    func inTrashFolder() -> Bool {
        do {
            let fm = FileManager.default
            let trashFolder = try fm.url(for: .trashDirectory, in: [], appropriateFor: self, create: false)
            var relationShip = FileManager.URLRelationship.other
            try fm.getRelationship(&relationShip, ofDirectoryAt: trashFolder, toItemAt: self)
            return relationShip == .contains
        } catch {
            return false
        }
    }
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382