0

In my app I picked files by UIDocumentPicker and put file names on a tableView. When clicking on a cell, I want the app open the file. I have no idea how to open the files picked before. Please help.

import UIKit

extension ViewController: UIDocumentMenuDelegate {
    func documentMenu(documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
        documentPicker.delegate = self

        self.presentViewController(documentPicker, animated: true, completion: nil)
    }
}

extension ViewController: UIDocumentPickerDelegate {
    func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
        if controller.documentPickerMode == UIDocumentPickerMode.Import {
          dispatch_async(dispatch_get_main_queue()) {

              if let fileName = url.lastPathComponent {

                self.files.append(fileName)

            }

          }            
        }      
    }        
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var files = [AnyObject]()

    @IBOutlet weak var fileTableView: UITableView!
    @IBAction func addDocuments(sender: AnyObject) {
        let importMenu = UIDocumentMenuViewController(documentTypes: ["public.data", "public.text"], inMode: .Import)

        importMenu.delegate = self

        self.presentViewController(importMenu, animated: true, completion: nil)

        let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.data", "public.text"], inMode: .Import)
        documentPicker.delegate = self
        documentPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen

        self.presentViewController(documentPicker, animated: true, completion: nil)
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    }

1 Answers1

2

You need to keep a reference to the entire URL, not just the filename.

Add the full url to self.files. Then update your cellForRowAtIndexPath to show just the lastPathComponent of that URL.

Then in didSelectRowAtIndexPath you have the access to the full URL of the file.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Hi rmaddy, again, thanks a lot for answering my question so quickly. I have modified my code as you mentioned above. Can you help for the code in didSeceltRowAtIndexPath? I have no clue how to open the file. – newdeveloper May 15 '16 at 14:52
  • Open the file in what way? What you are you actually trying to do with the file? Display it to the user for viewing? For editing? Do you just want to copy it? You need to clarify what you are trying to accomplish. – rmaddy May 15 '16 at 14:54
  • The idea is users pick files and can review any time later. So when clicking on a cell, the app can display the file to the user for viewing. Thanks. – newdeveloper May 15 '16 at 14:57
  • Look into using `QLPreviewController`, `UIWebView`, or `WKWebView`. – rmaddy May 15 '16 at 15:04
  • Ok, I will. Thanks for the clue. – newdeveloper May 15 '16 at 15:06
  • BTW - the edits you made to your question are inappropriate. It's now a completely different question and makes my answer pointless. – rmaddy May 15 '16 at 15:06
  • Sorry, I have no experience in this. I am gonna change the question back. Thanks for your help again and I am gonna accept your answer. – newdeveloper May 15 '16 at 15:14
  • Hi rmaddy, just let you know I have worked out and everything is perfect now. Thanks for your clues again! – newdeveloper May 21 '16 at 23:58