【发布时间】:2018-09-19 19:59:14
【问题描述】:
除了打开和保存自己的自定义文档之外,我的基于 iOS 11 文档浏览器的应用程序应该能够导入适当格式的文本文件并将其转换为自己的文档。
但是,当在文档浏览器中选择文本文件时,只有当所选文件在本地存储中时,应用才能访问其内容(然后执行转换),但 在以下情况下无法访问内容该文件存储在 iCloud 中。
下面提供了我用于documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) 委托方法的代码的简化注释版本:
func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) {
guard let sourceURL = documentURLs.first else { return }
// Check extension to verify if it is a custom document
if sourceURL.pathExtension == "mydoc" {
// If it is a custom document, we present it directly
// (the presentDocument method is a standard implementation from Apple docs)
presentDocument(at: sourceURL)
} else {
// Otherwise, we suppose the file is a text file to be imported
// We first create a new document, then try to import the contents of the text file
let url = FileManager().temporaryDirectory.appendingPathComponent("New Document").appendingPathExtension("mydoc")
let doc = MyDocument(fileURL: url)
do {
let textFileContents = try String(contentsOf: sourceURL)
// It works fine if the text file is in local storage
// Here the document model is updated by converting the contents of the text file
// (code omitted, it is actually a method in the MyDocument class)
// ...
} catch {
// Produce error message: cannot access text file
// The error message is always produced if the text file is in iCloud storage!
}
// Save and close the document with standard code, e.g.:
doc.save(to: url, for: .forCreating) { (saveSuccess) in
guard saveSuccess else { return }
doc.close(completionHandler: { (closeSuccess) in
guard closeSuccess else { return }
})
// Reveal and import the document
self.revealDocument(at: url, importIfNeeded: true) { (revealedDocumentURL, error) in
if let error = error {
// Handle the error appropriately
return
}
// Present the Document View Controller for the revealed URL
self.presentDocument(at: revealedDocumentURL!)
}
}
}
info.plist 文件将自定义文档和 public.text 声明为文档类型(具有适当的角色和处理程序等级),并将自定义文档声明为导出的 UTI。如果选择的文件是自定义文档,则一切正常,无论文件是本地文件还是 iCloud 中。
由于文件在本地存储中时导入工作正常,我认为这可能是 iCloud 权限的问题,即使 Apple 文档为 UIDocumentBrowserViewController 状态:
系统会自动为您处理对 iCloud 的访问;你不 需要启用您应用的 iCloud 功能。
但是,尝试将 iCloud 功能添加到我的应用程序的权利并没有解决问题(实际上让情况变得更糟,因为完全相同的代码现在正在默认保存新创建的文档应用程序的 iCloud 容器,不与文档浏览器使用的 iCloud Drive 位置相同,因此所有文档对浏览器都变得“不可见” - 但我离题了...)。
另一个“有趣”的元素是我还在应用程序中实现了UIDocumentPickerViewController,以将其他内容从文本文件导入到已经打开的自定义文档中。我使用的代码与上面的基本相同......并且它完美地工作,与文本文件是在本地存储还是在 iCloud 中无关!这可以强化这种观点,即问题与UIDocumentBrowserViewController 的特定权限问题有关。
所以,总而言之:我可以做些什么来访问存储在 iCloud 中的文本文件的内容并将它们转换为我的应用程序的自定义文档?提前感谢您的任何建议(如果问题的表述有问题,请保持温和 - 这是我潜伏数月后的第一个 stackoverflow 问题!)。
【问题讨论】:
标签: ios swift icloud icloud-drive uidocumentbrowservc