Search code examples
iphoneobjective-ciosios6

Handling of temporary files with UIDocumentInteractionController


I'm using the UIDocumentInteractionController with a temporary file that resides in my cache after having downloaded it. I'm using a quite simple class that delivers md5-cache file names (ext = cache) to my app and the downloaded file is in this format. The reason is to have files locally and to only download them once (a session). Since the cache names are in a uniform format I can easily clean them up.

Now with UIDocumentInteractionController I need to rename these files back to their original name or they will not get recognized correctly.

When the UIDocumentInteractionController finishes handing off the file I thought to move the file back to its cache file name. The problem is, the method: - documentInteractionController:didEndSendingToApplication: never gets called - though the delegate is set correctly.

How I basically set up the controller:

interactionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:temporaryFile]];
interactionController.delegate = self;
interactionController.annotation = [cacheURLString lastPathComponent]; // original name to move back to

Any suggestions on how to correctly find out that a file has been handed over to another application / or the UIDocumentInteractionController has been dismissed?


Solution

  • I found that documentInteractionController:didEndSendingToApplication: is called if the document is sent to another application, but isn't called when sending the document via email (and possibly other built in functions like AirDrop, copy, print, etc.). This seems like a bug to me, but there it is.

    Tony's answer in the comments worked for me - use [[NSFileManager defaultManager] linkItemAtURL:toURL:error] to link the source file to a temporary file and pass the temporary file to the controller. This isn't taking up a lot of additional space and the temporary link will be removed after a certain amount of time.