Introduction to Document Pickers and PDF Files on iOS
As a developer, uploading files from the user’s device is an essential feature for any mobile application. In this article, we will focus on how to list out PDF files using a document picker on iOS.
Understanding UIDocumentMenuViewController
The first step in listing out PDF files is to create a UIDocumentMenuViewController instance. This class allows you to present a menu of available documents that the user can choose from.
Creating a UIDocumentMenuViewController Instance
To create a UIDocumentMenuViewController, we need to specify the types of documents that we want to display in the menu. We do this by creating an array of document types using the UIDocumentTypeIdentifier class.
NSArray *docArr = @[
(NSString *) UIDocumentTypeIdentifierForFileType(kUTTypePDF),
(NSString *) UIDocumentTypeIdentifierForFileType(kUTTypeText),
(NSString *) UIDocumentTypeIdentifierForFileType(kUTTypeImage)
];
In this example, we are only displaying PDF files (kUTTypePDF), text files (kUTTypeText), and image files (kUTTypeImage) in the menu.
Configuring the UIDocumentMenuViewController
After creating the docArr, we need to configure the UIDocumentMenuViewController. We do this by setting the document types, mode, and delegate.
- (instancetype)initWithDocumentTypes:(NSArray<NSString *> *)documentTypes inMode:(UIDocumentPickerMode)mode {
self = [super init];
if (self) {
// ...
}
return self;
}
- (void)setDelegate:(id<UIDocumentMenuViewControllerDelegate>)delegate {
_delegate = delegate;
}
In this example, we are setting the document types to docArr, and setting the mode to UIDocumentPickerModeImport. This means that the user will be able to import documents from their device.
We also need to set a delegate for the view controller. The delegate is responsible for handling events such as when the user selects a document.
Implementing UIDocumentMenuViewControllerDelegate
To implement the UIDocumentMenuViewControllerDelegate, we need to conform to the UIDocumentMenuViewControllerDelegate protocol.
- (void)documentMenuViewController:(UIDocumentMenuViewController *)controller didFinishPickingDocumentsAtIndexPaths:(NSArray<NSIndexPath *> *)indexes {
// Handle the selected documents here
}
In this example, we are handling the event when the user selects a document. We can access the selected documents using the indexes array.
Presenting the UIDocumentMenuViewController
To present the UIDocumentMenuViewController, we need to use the presentViewController:animated:completion: method.
- (void)presentDocumentPicker {
// ...
[self presentViewController:self.myDocumentMenu animated:YES completion:^{
NSLog(@"Presented successfully");
}];
}
In this example, we are presenting the UIDocumentMenuViewController instance named myDocumentMenu.
Handling Document Selection
To handle document selection, we need to implement the documentMenuViewController:didFinishPickingDocumentsAtIndexPaths: method.
- (void)documentMenuViewController:(UIDocumentMenuViewController *)controller didFinishPickingDocumentsAtIndexPaths:(NSArray<NSIndexPath *> *)indexes {
// Get the selected documents
NSArray *documents = [self.myDocumentMenu documentedUrls];
// Handle the selected documents here
[controller dismissViewControllerAnimated:YES completion:nil];
}
In this example, we are getting the selected documents using the documentedUrls property. We can then handle the selected documents as needed.
Conclusion
Listing out PDF files using a document picker on iOS is an essential feature for any mobile application. By using the UIDocumentMenuViewController class, developers can present a menu of available documents that the user can choose from. With this knowledge, developers can create applications that allow users to upload and manage files.
Additional Considerations
In addition to listing out PDF files, developers may also want to consider other features such as:
- File preview: Displaying a preview of the selected file to give the user an idea of what they are selecting.
- File filtering: Filtering the available documents based on certain criteria such as file type or size.
- File handling: Handling the uploaded files in the application’s storage.
These features can enhance the overall user experience and provide a more intuitive way for users to interact with the application.
Example Use Case
Here is an example of how you might use the UIDocumentMenuViewController class in a real-world application:
// Create a new instance of UIDocumentMenuViewController
myDocumentMenu = [[UIDocumentMenuViewController alloc] initWithDocumentTypes:docArr inMode:UIDocumentPickerModeImport];
// Set the delegate for the view controller
self.myDocumentMenu.delegate = self;
// Present the view controller
[self presentViewController:self.myDocumentMenu animated:YES completion:^{
NSLog(@"Presented successfully");
}];
In this example, we are creating a new instance of UIDocumentMenuViewController with the specified document types and mode. We then set the delegate for the view controller to implement the required methods. Finally, we present the view controller using the presentViewController:animated:completion: method.
By following these steps, developers can create applications that allow users to upload and manage files in a user-friendly way.
Last modified on 2025-04-28