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?
-
what issue you getting, write here.vaibhav– vaibhav2018-01-29 05:40:09 +00:00Commented Jan 29, 2018 at 5:40
-
I get file url from UIDocumentPickerDelegate but i need to get file.Ajay Sangani– Ajay Sangani2018-01-29 05:41:34 +00:00Commented Jan 29, 2018 at 5:41
-
Edit your question to include your relevant code (as text) and clearly show where you need help.rmaddy– rmaddy2018-01-29 05:44:49 +00:00Commented Jan 29, 2018 at 5:44
-
What kind of file ? What is your solution so far ?Sudipto Roy– Sudipto Roy2018-01-29 05:45:03 +00:00Commented Jan 29, 2018 at 5:45
-
I need to get file like .pdf, .txt, .apk, etc. @RoyAjay Sangani– Ajay Sangani2018-01-29 05:48:07 +00:00Commented Jan 29, 2018 at 5:48
|
Show 9 more comments
2 Answers
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)
}
Comments
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")
}
}