从 USB-C 驱动器和 iPad iOS13 读取文件和内容
Posted
技术标签:
【中文标题】从 USB-C 驱动器和 iPad iOS13 读取文件和内容【英文标题】:Read files and contents from USB-C drive and iPad iOS13 【发布时间】:2019-10-08 13:26:44 【问题描述】:我创建了一个 UIDocumentPickerViewController 并在 USB 驱动器上选择了我的文件夹,在使用枚举器列出文件后,我如何读取内容?
你能读取文件的内容吗?
这是我的示例代码:
@IBAction func openFiles(_ sender: Any)
// Using the Document Picker to Pick a Folder
let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeFolder as String], in: .open)
documentPicker.delegate = self
documentPicker.shouldShowFileExtensions = true
present(documentPicker, animated: true, completion: nil)
@IBAction func readFiles()
// Reading the Content of a Picked Folder
let shouldStopAccessing = pickedFolderURL.startAccessingSecurityScopedResource()
defer
if shouldStopAccessing
pickedFolderURL.stopAccessingSecurityScopedResource()
var coordinatedError:NSError?
NSFileCoordinator().coordinate(readingItemAt: pickedFolderURL, error: &coordinatedError) (folderURL) in
let keys : [URLResourceKey] = [.nameKey, .isDirectoryKey]
let fileList = FileManager.default.enumerator(at: pickedFolderURL, includingPropertiesForKeys: keys)!
logString = ""
for case let file as URL in fileList
let newFile = file.path.replacingOccurrences(of: pickedFolderURL.path, with: "")
if(newFile.hasPrefix("/.") == false) //exclude hidden
print(file)
logString += "\n\(file)"
if (file.pathExtension == "mp4")
self.pickedVideoURL = file
if (file.pathExtension == "txt")
self.pickedTextURL = file
self.logTextView.text = logString
现在我想读取 txt/mp4 文件的内容...如何? 使用 quicklook 或 AVPlayerViewController 我得到阅读错误...
你有示例项目吗?
【问题讨论】:
我在这里有几乎完全相同的问题——***.com/questions/60498625/… 你必须使用securityScope和coordinator的组合。 【参考方案1】:解决方案是结合使用securityScope和文件NSFileCoordinator
@IBAction func readFiles()
// Reading the Content of a Picked Folder
// pickedFolderURL must be a fileURL
let shouldStopAccessing = pickedFolderURL.startAccessingSecurityScopedResource()
defer
if shouldStopAccessing
pickedFolderURL.stopAccessingSecurityScopedResource()
var coordinatedError:NSError?
NSFileCoordinator().coordinate(readingItemAt: pickedFolderURL, error: &coordinatedError) (folderURL) in
let keys : [URLResourceKey] = [.nameKey, .isDirectoryKey]
let fileList = FileManager.default.enumerator(at: pickedFolderURL, includingPropertiesForKeys: keys)!
for case let file as URL in fileList
guard pickedFolderURL.startAccessingSecurityScopedResource() else
fatalError("Handle the failure here.")
return;
// Make sure you release the security-scoped resource when you are done.
defer pickedFolderURL.stopAccessingSecurityScopedResource()
let newFile = file.path.replacingOccurrences(of: pickedFolderURL.path, with: "")
if(newFile.hasPrefix("/.") == false) //exclude hidden
print(file)
self.logTextView.text = logString
【讨论】:
您的意思是再次guard pickedFolderURL
还是guard file
?无论哪种方式 - 我尝试对文件进行安全范围限制,并且总是在 fatalError
部分结束
确定文件夹的范围将使您能够递归地访问整个子文件夹...我认为错误在别处
@Kal 无论如何我再次尝试了这段代码,它在没有权限错误的情况下工作。以上是关于从 USB-C 驱动器和 iPad iOS13 读取文件和内容的主要内容,如果未能解决你的问题,请参考以下文章
USB-C智能快充音箱方案,支持PD 100W输入,对外输出5V供电读U盘
USB-C智能快充音箱方案,支持PD 100W输入,对外输出5V供电读U盘
如何使用 iOS 13 在 iPad/iPhone 上隐藏页面缩放栏