如何打开 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 文件。
在我的应用程序中,我通过 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 文件。