— UIDocumentPickerViewController, Swift, File Selection — 1 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.
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.
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 UIKit2import MobileCoreServices
The MobileCoreServices
framework provides constants for UTIs (Uniform Type Identifiers), which are used to identify different types of files.
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 = self6 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.
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 URL7 print("Selected file URL: \(selectedFileURL)")8 }9
10 func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {11 // User cancelled the document picker12 }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.
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.