UIDocument's presentedItemDidMoveToURL: is called even though the presented file was not moved, sometimes with an invalid URL

Number:rdar://FB7547846 Date Originated:Jan 23, 2020 at 12:28 PM
Status:Open Resolved:
Product:UIKit Product Version:
Classification: Reproducible:
If an app is shown in the "On my iPhone/iPad" location in the Files app and a file is opened using UIDocument, the system calls presentedItemDidMoveToURL: on the document when the app comes to the foreground, even if the file was in fact not moved. The new file url has a "private/var" prefix instead of "var".

Furthermore, and much more critical, the new url is invalid if the file is stored a folder with certain names:

For example, if the file is stored in Documents/Test.tmbundle/test.txt, then presentedItemDidMoveToURL: is called with the path Documents/Test.tmbundle/ which is the path of the directory instead of the file. This results in an error trying to open the directory as a file.

I could isolate the issue in a small sample project that is attached to this feedback.

Step to reproduce:
- The app's document directory must be visible in the Files app, so enable "UIFileSharingEnabled" and "LSSupportsOpeningDocumentsInPlace" in the app's Info.plist file.
- Using an UIDocument subclass, open a file called "text.txt" in the subfolder called "Test.tmbundle" in the app's document folder.
- Leave the app using the home button/indicator.
- Open the app again
- presentedItemDidMoveToURL: is called on the document subclass even though the file was not moved.

Here is a sample log from the sample project:

opened file file:///var/mobile/Containers/Data/Application/9854C1E4-07BE-4E18-865A-2E77D4F66039/Documents/Test.tmbundle/test.txt, success: 1

file:///private/var/mobile/Containers/Data/Application/9854C1E4-07BE-4E18-865A-2E77D4F66039/Documents/Test.tmbundle/, old url: 

As you can see, the new URL contains "private/var" instead of "var" at the beginning AND the URL points to the folder named "Test.tmbundle" instead of the actual opened file (test.txt) which is contained within that folder.

In a real app, this obviously results in big problems.

The problem with the stripped file name happens  when the folder is named "Test.tmbundle", "Test.pages" or "Test.bundle". It does not happen when the folder is called "Test.txt" or "Test.folder". In that case, presentedItemDidMoveToURL: is called with a "private/var" URL (which is also a bug), but at least the file name is not stripped from the URL.

FYI, the sample project can also log info about the .tmbundle extension in ViewController.m. It does not have an UTI type declaration on my device:
uti of .tmbundle is dyn.age81k5pcsz1gk5df
type declaration: (null)

The device is running iOS 13.3.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!