UIDocumentPickerViewController
A view controller that provides access to documents or destinations outside your app’s sandbox.
Declaration
@MainActor class UIDocumentPickerViewControllerMentioned in
Overview
Use a document picker view controller to select a document to open or export, and optionally copy. Don’t copy the document if you can avoid it. The document picker operates in two modes:
Open a document. The user selects a document. The document picker provides access to the document, and the user can edit the document in place. Optionally, you can specify that the document picker makes a copy of the document, leaving the original unchanged.
Export a local document. The user selects a destination. The document picker moves the document, and the user can access it and edit it in place. Optionally, you can specify that the document picker makes a copy of the document, leaving the original unchanged.
Work with external documents
Both the open and export operations grant access to documents outside your app’s sandbox. This access gives users an unprecedented amount of flexibility when working with their documents. However, it also adds a layer of complexity to your file handling. External documents have the following additional requirements:
The open and move operations provide security-scoped URLs for all external documents. Call the startAccessingSecurityScopedResource() method to access or bookmark these documents, and the stopAccessingSecurityScopedResource() method to release them. If you’re using a
UIDocumentsubclass to manage your document, it automatically manages the security-scoped URL for you.Always use file coordinators (see NSFileCoordinator) to read and write to external documents.
Always use a file presenter (see NSFilePresenter) when displaying the contents of an external document.
Don’t save URLs that the open and move operations provide. You can, however, save a bookmark to these URLs after calling startAccessingSecurityScopedResource() to ensure you have access. Call the bookmarkData(options:includingResourceValuesForKeys:relativeTo:) method and pass in the withSecurityScope option, creating a bookmark that contains a security-scoped URL.
For more information about working with external documents, see Providing access to directories and Adding a document browser to your app.
Topics
Creating a document picker
init(coder:)init(forExporting:)init(forExporting:asCopy:)init(forOpeningContentTypes:)init(forOpeningContentTypes:asCopy:)
Getting the user-selected document
Configuring a document picker
Deprecated
Initializers
See Also
Documents and directories
Customizing a document-based app’s launch experienceAdding a document browser to your appProviding access to directoriesBuilding an app with a document browserBuilding a document browser app for custom file formatsUIDocumentViewControllerUIDocumentBrowserViewControllerUIDocumentInteractionController