如何打开UIDocumentPicker导入的文件



在我的应用程序中,我按UIDocumentPicker挑选文件,并将文件名放在tableView上。单击cell时,我希望应用程序打开该文件。我不知道如何打开以前挑选的文件。请帮忙。

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

您需要保留对整个URL的引用,而不仅仅是文件名。

将完整的url添加到self.files。然后更新您的cellForRowAtIndexPath以仅显示该URL的lastPathComponent

然后在didSelectRowAtIndexPath中,您可以访问文件的完整URL。

相关内容

  • 没有找到相关文章

最新更新