1

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)
        }
    }
}
aleksy.t
  • 267
  • 2
  • 18
  • https://developer.apple.com/documentation/uikit/uidocumentpickerdelegate/1618680-documentpicker is deprecated. You should use `optional func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])`. https://developer.apple.com/documentation/uikit/uidocumentpickerdelegate/2902364-documentpicker – Leo Dabus Jun 12 '19 at 15:54
  • try declaring `documentBrowserController` as a property of your view controller. (move it out of browseFiles method to your EditViewTableViewController declaration) – Leo Dabus Jun 12 '19 at 15:55

0 Answers0