Class

UIDocumentPickerViewController

A view controller that provides access to documents or destinations outside your app’s sandbox.

Overview

You must enable iCloud document support before you can use the document picker. For more information, see iCloud Design Guide.

Document Picker Modes

The document picker supports four modes. Each mode provides a slightly different approach to sharing files between apps.

  • Import an external document. The user selects an external document. The document picker copies the document, leaving the original unchanged.

  • Export a local document. The user selects an external destination. The document picker copies the document, leaving the original unchanged.

  • Open an external document. The user selects an external document. The document picker provides access to the document, and the user can edit the document in place.

  • Move a local document. The user selects an external destination. The document picker moves the document; however, you can still access the document as an external document, and the user can edit the document in place.

Working with External Documents

Both the open and move 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 these documents and the stopAccessingSecurityScopedResource() method to release them. If you are using a UIDocument subclass to manage your document, this subclass automatically manages the security-scoped URL for you.

  • Always use file coordinators to read and write to external documents.

  • Always use a file presenter when displaying the contents of an external document.

  • Don’t save URLs provided by the open and move operations. You can, however, save a bookmark to these URLs. Call the bookmarkData(options:includingResourceValuesForKeys:relativeTo:) method and pass in the withSecurityScope option, creating a bookmark containing a security-scoped URL.

For more about working with external documents, see Creating an Outstanding User Experience in App Extension Programming Guide.

Topics

Creating a Document Picker

init(documentTypes: [String], in: UIDocumentPickerMode)

Initializes and returns a document picker that can import or open the given file types.

init(url: URL, in: UIDocumentPickerMode)

Initializes and returns a document picker that can export or move the given document.

init?(coder: NSCoder)

Returns an object initialized from data in a given unarchiver.

Getting the User-Selected Document

var delegate: UIDocumentPickerDelegate?

The document picker’s delegate.

var allowsMultipleSelection: Bool

A Boolean value that determines whether the user can select more than one document at a time.

protocol UIDocumentPickerDelegate

A set of methods that you implement to track when the user selects a document or destination, or to track when the document picker is canceled.

Configuring a Document Picker

var documentPickerMode: UIDocumentPickerMode

The type of file-transfer operation that the document picker performs.

enum UIDocumentPickerMode

Modes that define the type of file-transfer operation used by the document picker.

See Also

Document Browser

Adding a Document Browser to Your App

Give users access to their local or remote documents from within your app.

class UIDocumentBrowserViewController

A view controller for browsing and performing actions on documents stored locally and in the cloud.