Skip to content
DeveloperMemos

Using UIDocumentPickerViewController for Selecting Files

UIDocumentPickerViewController, Swift, File Selection1 min read

In iOS app development, there are often scenarios where you need to allow users to select files from their device's file system. The UIDocumentPickerViewController class provides a convenient way to implement file selection functionality within your app. In this tutorial, we will explore how to use UIDocumentPickerViewController in Swift by following along with some examples.

Setting Up the Project

Before we dive into using UIDocumentPickerViewController, let's set up a basic project structure. Open Xcode and create a new iOS project using the Single View App template. Choose a suitable name and options for your project, and make sure to select Swift as the language.

Importing the Required Frameworks

To use UIDocumentPickerViewController, we need to import the necessary frameworks. Open the view controller file (e.g., ViewController.swift) and add the following import statement at the top:

1import UIKit
2import MobileCoreServices

The MobileCoreServices framework provides constants for UTIs (Uniform Type Identifiers), which are used to identify different types of files.

Presenting the Document Picker

To present the document picker interface, we'll create a button in our view controller and handle its tap event. Add the following code inside the view controller class:

1class ViewController: UIViewController {
2
3 @IBAction func selectFileButtonTapped(_ sender: UIButton) {
4 let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.pdf])
5 documentPicker.delegate = self
6 present(documentPicker, animated: true, completion: nil)
7 }
8
9}

In this example, we create an instance of UIDocumentPickerViewController and set its documentTypes property to specify the types of files that can be selected. Here, we restrict the selection to PDF files using the kUTTypePDF constant from the MobileCoreServices framework.

Handling the Document Picker Delegate

To handle the result of file selection, we need to conform to the UIDocumentPickerDelegate protocol. Add the following extension at the bottom of the view controller class:

1extension ViewController: UIDocumentPickerDelegate {
2
3 func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
4 guard let selectedFileURL = urls.first else { return }
5
6 // Handle the selected file URL
7 print("Selected file URL: \(selectedFileURL)")
8 }
9
10 func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
11 // User cancelled the document picker
12 }
13
14}

In this example, we implement the documentPicker(_:didPickDocumentsAt:) method to handle the selected file URL. You can perform further operations with the selected file, such as uploading it to a server or processing its contents.

Summary

In this tutorial, we learned how to use UIDocumentPickerViewController in Swift to enable file selection within your iOS app. We explored how to present the document picker interface and handle the selected file URL using the delegate methods. With this knowledge, you can now incorporate file selection functionality into your own iOS projects.