I'm working on attaching the file to an e-mail and I want to do it through UIDocumentPickerViewController. Basically, the user clicks on the icon and he can select a file. Everything worked, the user could select a file and I could get the url to it and later on make a file transfer. Recently it stopped working and surprisingly, I haven't made any changes to the code (Yes, it sounds silly). What I spotted is that my didPickDocumentAt delegate isn't called. All in all, browseFiles function is called on click and documentBrowser is presented but selecting a file doesn't work (didPickDocumentAt delegate function & uploadDataFromUrl isn't called)
extension EditViewTableViewController: UIDocumentMenuDelegate, UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
self.uploadDataFromUrl(url: url)
}
public func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
dismiss(animated: true, completion: nil)
}
func browseFiles() {
let documentBrowserController = UIDocumentPickerViewController(documentTypes: [kUTTypeImage, kUTTypeCompositeContent] as [String], in: UIDocumentPickerMode.import)
if #available(iOS 11.0, *) {
documentBrowserController.allowsMultipleSelection = true
}
documentBrowserController.delegate = self
self.present(documentBrowserController, animated: false, completion: nil)
}
func uploadDataFromUrl(url: URL) {
if FileManager.default.fileExists(atPath: url.path) {
do {
let data = try Data(contentsOf: url)
self.didUploadedTheFile(path: url.path)
} catch {
print(error.localizedDescription)
}
}
}