The documentation for documentBrowser(_:didRequestDocumentCreationWithHandler:)
says, "Create a new document and save it to a temporary location. If you use a UIDocument
subclass to create the document, you must close it before calling the importHandler
block."
So I created a file URL by taking the URL for the user's temporary directory (FileManager.default.temporaryDirectory
) and appending a name and extension (getting a path like "file:///private/var/mobile/Containers/Data/Application/C1DE454D-EA1E-4166-B137-5B43185169D8/tmp/Untitled.uti"). But when I call save(to:for:completionHandler:)
passing this URL, the completion handler is never called back. I also tried using url(for:in:appropriateFor:create:)
to pass a subdirectory in the user's temporary directory—the completion handler was still never called.
I understand the document browser view controller is managed by a separate process, which has its own read / write permissions. Beyond that though, I'm having a hard time understanding what the problem is. Where can new documents be temporarily saved so that the document browser process can move them?
Update: as of the current betas, I now see an error with domain NSFileProviderInternalErrorDomain
and code 1
getting logged: "The reader is not permitted to access the URL." At least that's confirmation of what's happening…
I had the same issue, but then I realized that the recommended way was to simply copy the package/folder from the Bundle, like so:
To clarify, this package is just a folder that you've created and plopped into Xcode.
This approach makes sense, if you think about it, for a few reasons:
Apple File System (AFS). The move towards AFS means copying is (almost) free.
Permissions. Copying from the Bundle is always permissible, and the user is specifying the location to copy to.
New document browser paradigm. Since we're using the new
UIDocumentBrowserViewController
paradigm (which is due to iOS11 and the new Files app), it is even handling the naming (c.f. Apple's Pages) and moving and arranging of files. We don't have to worry about which thread to run things on either.So. Simpler, easier, and probably better. I can't think of a reason to manually create all the files (or use the temp folder, etc).