1

I have integrated UIDocumentPickerDelegate or UIDocumentMenuDelegate, I have an issue to get the file from URL or DocumentPicker. How can I solve this issue in iOS swift?

Akash More
  • 143
  • 1
  • 2
  • 13
Ajay Sangani
  • 21
  • 1
  • 7

2 Answers2

5

You can get file easily like,

If you need to get image

    @IBAction func btn_Handler_iCloud(_ sender: Any) {
        let doc = UIDocumentMenuViewController(documentTypes: [String(kUTTypeImage)], in: .import)
        doc.delegate = self
        doc.modalPresentationStyle = .formSheet
        self.present(doc, animated: true, completion: nil)
    }

    func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
        documentPicker.delegate = self
        present(documentPicker, animated: true, completion: nil)
    }

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        let data = try! Data(contentsOf: urls[0])
        imageview_Buaty.image = UIImage.init(data: data)
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        dismiss(animated: true, completion: nil)
    }
Kamani Jasmin
  • 691
  • 8
  • 11
1

Getting document URL or data from UIDocumentPickerViewController:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) { 
    // Available from iOS 8.0 to iOS 11.0
    self.handleFileSelection(inUrl: url)// Here url is document's URL
}

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    // Available from iOS 11.0
    self.handleFileSelection(inUrl: urls.first!)  // Here urls is array of URLs
}

private func handleFileSelection(inUrl:URL) -> Void {
    do { 
     // inUrl is the document's URL
        let data = try Data(contentsOf: inUrl) // Getting file data here
    } catch {
        dLog(message: "document loading error")
    }
}
Shahul Hasan
  • 300
  • 3
  • 2