Implement Document Picker in swift (iOS)
Update for iOS 14: You do not need any capabilities. Just create a UIDocumentPickerViewController
with the appropriate types, implement the delegate, and you are done.
More info in this answer. Code from there:
import UIKit
import MobileCoreServices
import UniformTypeIdentifiers
func selectFiles() {
let types = UTType.types(tag: "json",
tagClass: UTTagClass.filenameExtension,
conformingTo: nil)
let documentPickerController = UIDocumentPickerViewController(
forOpeningContentTypes: types)
documentPickerController.delegate = self
self.present(documentPickerController, animated: true, completion: nil)
}
From your project's capabilities, enable both the iCloud
and the Key-Sharing
.
Import MobileCoreServices
in your class and then extend the following three classes inside your UIViewController
:
UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate
Implement the following functions:
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let myURL = urls.first else {
return
}
print("import result : \(myURL)")
}
public func documentMenu(_ documentMenu:UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("view was cancelled")
dismiss(animated: true, completion: nil)
}
How do you call all of this? Add the following bit of code to your click function:
func clickFunction(){
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
Click your button. The following menu will pop up ..
In the case of Dropbox. Upon clicking on any item. You will be redirected back to your app and the URL will be logged in your terminal.
Manipulate the documentTypes to your need. In my app, Users permitted to Pdf only. So, suit yourself.
kUTTypePDF
kUTTypePNG
kUTTypeJPEG
...
Also if you feel like customizing your own menu bar. Add the following code and customize your own function inside the handler
importMenu.addOption(withTitle: "Create New Document", image: nil, order: .first, handler: { print("New Doc Requested") })
Enjoy it.
You can use UIDocumentPickerViewController
to get the files from the Files apps or iCloud Drive.
- Activate the
Key-value storage
andiCloud Documents
fromiCloud
capability:
Import the following framework on the view controller you want to open the document picker:
import MobileCoreServices // For iOS 14+ import UniformTypeIdentifiers
Implement the following method from
UIDocumentPickerDelegate
:func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { // you get from the urls parameter the urls from the files selected }
Create an
UIDocumentPickerViewController
to display the File picker or iCloud Drive:// Use this code if your are developing prior iOS 14 let types: [String] = [kUTTypePDF as String] let documentPicker = UIDocumentPickerViewController(documentTypes: types, in: .import) documentPicker.delegate = self documentPicker.modalPresentationStyle = .formSheet self.present(documentPicker, animated: true, completion: nil) // For iOS 14+ let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [UTType.item], asCopy: false) documentPicker.delegate = self documentPicker.modalPresentationStyle = .formSheet self.present(documentPicker, animated: true, completion: nil)
If you want the user can import more types of files to your app, you have to add more UTType
s to the types
NSArray
. To see all the types available, you can check the UTType Docs
When the document picker opens on iOS 11 and you try to select a file inside the Google Drive, it is possible the file is disable due a bug: http://www.openradar.me/24187873