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?

14
  • what issue you getting, write here. Commented Jan 29, 2018 at 5:40
  • I get file url from UIDocumentPickerDelegate but i need to get file. Commented Jan 29, 2018 at 5:41
  • Edit your question to include your relevant code (as text) and clearly show where you need help. Commented Jan 29, 2018 at 5:44
  • What kind of file ? What is your solution so far ? Commented Jan 29, 2018 at 5:45
  • I need to get file like .pdf, .txt, .apk, etc. @Roy Commented Jan 29, 2018 at 5:48

2 Answers 2

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)
    }
Sign up to request clarification or add additional context in comments.

Comments

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")
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.