簡體   English   中英

如何打開UIDocumentPicker導入的文件?

[英]How to open files imported by 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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM