0

How get file url from UIDocumentPickerController, for later upload to my server? Sending files as implemented Telegram. Now there is only the possibility to download a file from icloud and send later. But I want to learn how to implement in the telegram?

This is my code now:

func openPicker() {
    let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.data"], inMode: UIDocumentPickerMode.Open)
    documentPicker.delegate = self
    documentPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen
    self.presentViewController(documentPicker, animated: true, completion: nil)
}

func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
    if controller.documentPickerMode == UIDocumentPickerMode.Open {
        let fileManager = NSFileManager.defaultManager()
        print(fileManager.fileExistsAtPath(url.path!))
        let data = NSData(contentsOfFile: url.path!)
        let document = UIDocument(fileURL: url)
    }
}
Zept
  • 1,158
  • 1
  • 10
  • 14

1 Answers1

0

This worked for me, I was able to fetch the url of the file and read contents from it without having to download the file

let documentPickerController = UIDocumentPickerViewController(documentTypes: [ICLOUD_DOCUMENT_SEARCH_TYPE], inMode: UIDocumentPickerMode.Import)
        documentPickerController.delegate = self
        self.presentViewController(documentPickerController, animated: true, completion: nil)

func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
    print(url)
}

From the url, you can then use NSFileManager to read contents of the file.

Rakshith Nandish
  • 599
  • 3
  • 13