0

I have defined a UTI for a custom document format. I can export files from my app and append them to text messages, email, etc. I can import the files into my app by tapping on the document icon in iMessage. By tapping on the document icon, I have the option to copy to my app. That triggers a call in my AppDelegate to handle the incoming file.

What's bugging me is that the url for the incoming file is:

file:///private/var/mobile/Containers/Data/Application/21377C94-1C3C-4766-A62A-0116B369140C/Documents/Inbox/...

Whereas, when saving documents to the .documents directory, I use this URL:

file:///var/mobile/Containers/Data/Application/21377C94-1C3C-4766-A62A-0116B369140C/Documents/...

The difference being the /private/ and /Inbox/ path components.

Question: how can I purge the /private/.../Inbox/ path of the files that were copied to my app from iMessage? I noticed this when testing my app and when I tapped on the same document icon in iMessage it started generating file copies with the same name but adding -1, then -2, then -3 to the file name of the document from iMessage. It appears that copies are building up in that /private/.../Inbox/ path.

Is that something that gets flushed on its own or can I access that directory to remove those files? It's also annoying because based upon the filename, it appears to be a different file thus allowing multiple copies of the same file to be imported with a slightly different file name.

Diskprotek
  • 601
  • 2
  • 7
  • 13

1 Answers1

0

Ok, this took a fair amount of digging, but I'll post my solution that seems to work thus far in case anyone runs across the same problem.

let fileManager = FileManager.default
// get the URL for the "Inbox"
let tmpDirURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("Inbox")
// get all the files in the "Inbox" directory
let anythingThere = try? fileManager.contentsOfDirectory(at: tmpDirURL, includingPropertiesForKeys: nil)
if anythingThere?.count ?? 0 > 0 {
   for eachURL in anythingThere! {
       // for each url pointing to a file in that directory, get its path extension
       let pathExtension = eachURL.pathExtension
       // test to see if it's a UTI that you're interested in deleting
       // in my case, the three "OCC" strings are the relevant UTI extensions
       if pathExtension == "OCCrcf" || pathExtension == "OCCrdi" || pathExtension == "OCCsrf" {
            // attempt to delete the temporary file that was copied to the 
            // "Inbox" directory from importing via email, iMessage, etc.
            try fileManager.removeItem(at: eachURL)
       }
   }
}

If anyone has a more elegant solution, please respond as well. Thanks.

Diskprotek
  • 601
  • 2
  • 7
  • 13