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?
Asked
Active
Viewed 4,474 times
1

Akash More
- 143
- 1
- 2
- 13

Ajay Sangani
- 21
- 1
- 7
-
what issue you getting, write here. – vaibhav Jan 29 '18 at 05:40
-
I get file url from UIDocumentPickerDelegate but i need to get file. – Ajay Sangani Jan 29 '18 at 05:41
-
[Edit] your question to include your relevant code (as text) and clearly show where you need help. – rmaddy Jan 29 '18 at 05:44
-
What kind of file ? What is your solution so far ? – roy Jan 29 '18 at 05:45
-
I need to get file like .pdf, .txt, .apk, etc. @Roy – Ajay Sangani Jan 29 '18 at 05:48
-
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) { let cico = url as URL print("The Url is : \(cico)") – Ajay Sangani Jan 29 '18 at 05:50
-
UIDocumentPicker will returns you the file URL, from that URL you needs to create the file or download the file from that URL your self. – Vandit Mehta Jan 29 '18 at 05:52
-
How can create the file or download the file from url in iOS swift? @VanditMehta – Ajay Sangani Jan 29 '18 at 05:54
-
like, if URL of image then you should write, UIImage *receipt = [UIImage imageWithContentsOfFile:url.path]; – Vandit Mehta Jan 29 '18 at 06:08
-
Yes, I know this way but How can get file from this way in iOS? @VanditMehta – Ajay Sangani Jan 29 '18 at 06:10
-
I don't know if this comment help you or not but I think when you get file url, you need to show that url data in web view. because web view support all kind of data to show. – bittu Jan 29 '18 at 06:53
-
Yes, i know but in my case i can send file in chat. @bittu – Ajay Sangani Jan 29 '18 at 07:02
-
Then I think as @VanditMehta suggest you can create image type object for image, audio type object for audio etc and send it over chat. Other than that I don't think there is any other option. – bittu Jan 29 '18 at 07:16
-
Any success on this ? – Sujit Baranwal Jul 10 '19 at 09:59
2 Answers
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