Document Picker Extensions in iOS: What They Are
In this article
Document picker extensions let an app contribute files to iOS's system-wide document picker, so users can browse and open those files from any app that uses the standard picker interface. They are a type of app extension introduced in iOS 8, and they are distinct from the document picker view controller that apps present to let users choose files. In short, a document picker extension makes your app's files available system-wide, while the picker view controller is the UI that displays those files.
Background and Purpose
Before iOS 8, sharing files between apps was limited. Each app had its own sandbox, and moving a document from one app to another usually required a workaround like emailing it to yourself or using a file-sharing service. Apple addressed this by introducing the document picker and its companion extension point. The idea was to create a standard way for apps to expose their documents to the system, so any app that uses the document picker can access files from any other app that provides a document picker extension.
The extension runs in a separate process from its containing app. This separation is a core iOS security feature: it limits what the extension can access and prevents a malicious extension from compromising the host app. The extension only sees the file URLs that the user explicitly selects, and it has no access to the host app's data or the rest of the system.
How a Document Picker Extension Works
When a user taps the “Browse” button in a document picker, iOS lists all available document picker extensions. If your app provides one, it appears as a source, often with an icon and name. When the user selects your source, iOS launches your extension in a separate process. The extension then presents its own user interface—typically a view controller that lists the files your app can provide.
The extension communicates with the host app through a set of delegate methods. For example, when the user selects a file, the extension calls a method to provide the file’s URL to the host. The host then imports a copy of that file into its own sandbox, so the original remains untouched. This is a key point: the host does not get direct access to your app's container; it gets a copy of the selected file.
To implement a document picker extension, you add a new target to your Xcode project, choose the “Document Picker Extension” template, and then implement a principal view controller that conforms to UIDocumentPickerExtensionViewController. That class provides methods like providePlaceholder(at:) and startProviding(at:) that you override to supply the actual file data.
Key Classes and Protocols
The main classes you work with are:
UIDocumentPickerExtensionViewController– the base class for your extension's main view controller.UIDocumentPickerViewController– the system-provided picker that your app presents when it wants to let the user choose a document.UIDocumentPickerDelegate– the protocol that your app implements to receive the selected file URLs.NSFileCoordinatorandNSFilePresenter– used to coordinate access to files, especially when files are provided on demand.
In practice, you rarely need to subclass UIDocumentPickerViewController; you just present it and set its delegate. The extension, on the other hand, is where you build the custom UI that shows your app's files.
How to Choose Between an Extension and Other Options
If you want your app's files to be accessible from other apps, a document picker extension is the right choice. But there are alternatives:
- File Sharing (iTunes File Sharing): Lets users copy files to your app via Finder (on macOS Catalina and later) or iTunes. It's simpler but only works when the device is connected to a computer.
- UIFileSharingEnabled: This Info.plist key enables file sharing, but it's not a replacement for a document picker extension; it just makes your app's Documents folder visible in the Files app.
- Files app integration: Starting with iOS 11, the Files app can browse your app's Documents folder if you set
UISupportsDocumentBrowserto YES. This is different from a document picker extension because it appears in the Files app, not in the document picker inside other apps.
In short, use a document picker extension when you want other apps to be able to open files that your app manages. Use Files app integration when you want users to manage files directly in the Files app.
Common Problems and Pitfalls
One frequent issue is that the extension doesn't appear in the document picker. This usually happens because the extension's Info.plist is missing the required NSExtensionPointIdentifier key, or because the extension's principal class is not set correctly. Another common problem is that the extension crashes when it tries to access files outside its container. Remember: the extension has its own sandbox, so it must use security-scoped URLs and coordinate access with NSFileCoordinator.
Another pitfall is forgetting to implement providePlaceholder(at:) correctly. This method must create a placeholder file that tells iOS the file exists and its metadata. If you skip it, the document picker may not show your files at all.
Performance can also be an issue. If your extension takes too long to provide a file, the system may kill it. Always provide files asynchronously and use progress reporting via NSProgress so the user sees a progress bar.
Security and Privacy Considerations
Document picker extensions run in a sandbox, but that doesn't mean they are automatically safe. The extension can access only the files it creates or that the user explicitly selects. However, you must be careful not to expose sensitive data in the file listing. For example, if your app stores passwords or health data, don't list those files in the document picker.
Also, be aware that the host app gets a copy of the selected file. If your file contains sensitive information, the user is deliberately sharing it, so that's acceptable. But you should not provide files that the user hasn't explicitly chosen.
Another security point: always use NSFileCoordinator to read and write files. This ensures that your extension and the host app don't conflict, and it respects file coordination across processes.
Frequently Asked Questions
Do document picker extensions work on iPad and iPhone?
Yes, they work on both. The document picker UI is available on iPhone and iPad, and extensions appear in the same way.
Can a document picker extension access iCloud Drive?
No. iCloud Drive is handled by the system's own document picker, not by third-party extensions. Your extension can access files in your app's container, including those stored in iCloud if you use iCloud Documents, but you must implement that yourself.
Is a document picker extension the same as a File Provider extension?
No. A File Provider extension (introduced in iOS 11) is a more powerful way to expose files to the Files app, and it can also appear in the document picker. But it's a different extension point with different capabilities, such as providing file enumeration and content in a more efficient way.
Can I use a document picker extension to let users open files from my app in other apps?
Yes, that's exactly what it does. When a user selects a file from your extension, the host app receives a copy of that file and can open it.
Do I need to implement both a document picker extension and a File Provider extension?
Not necessarily. If you only need to provide files to other apps, a document picker extension is sufficient. If you want your files to appear in the Files app with full browsing and searching, a File Provider extension is better. You can have both, but that's more work.