【发布时间】:2021-04-14 15:46:49
【问题描述】:
我正在尝试构建与 Pages / Numbers / Keynote 具有类似行为的 iOS 应用程序。这些应用程序中的每一个都是基于文档的应用程序,其中首先向用户呈现UIDocumentBrowserViewController,用户可以在其中选择要在应用程序中打开的文档。例如,在 Numbers 中,用户可以选择 .numbers 文件并将其打开,或者用户可以选择 .csv 并将此 csv 文件导入数字文件,该文件与原始 csv 一起保存在同一位置.
在我的应用程序中,我希望用户选择一个.csv 文件,然后将其导入我自己的文档格式(称为.pivot)并将其与 csv 文件一起保存(就像数字一样)。在模拟器中运行良好,但是当我在设备上运行代码时,在自定义 Pivot 文档上调用 save(to:for:completionHandler:) 时出现错误。
我的文档浏览器代码如下。
class DocumentBrowserViewController: UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
allowsDocumentCreation = false
allowsPickingMultipleItems = false
}
func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentsAt documentURLs: [URL]) {
guard let sourceURL = documentURLs.first else { return }
if sourceURL.pathExtension == "csv" {
// Create a CSV document so we can read the CSV data
let csvDocument = CSVDocument(fileURL: sourceURL)
csvDocument.open { _ in
guard let csv = csvDocument.csvData else {
fatalError("CSV is nil upon open")
}
// Create the file at the same location as the csv, with the same name just a different extension
var pivotURL = sourceURL.deletingLastPathComponent()
let pivotFilename = sourceURL.lastPathComponent .replacingOccurrences(of: "csv", with: "pivot")
pivotURL.appendPathComponent(pivotFilename, isDirectory: false)
let model = PivotModel()
model.csv = csv
let document = PivotDocument(fileURL: pivotURL)
document.model = model
document.save(to: pivotURL, for: .forCreating, completionHandler: { success in
// `success` is false here
DispatchQueue.main.async {
self.performSegue(withIdentifier: "presentPivot", sender: self)
}
})
}
}
}
}
我第一个加载csv文件的UIDocument子类如下。
import SwiftCSV // This is pulled in using SPM and works as I expect, so is unlikely causing this problem
class CSVDocument: UIDocument {
var csvData: CSV?
override func contents(forType typeName: String) throws -> Any {
return Data()
}
override func load(fromContents contents: Any, ofType typeName: String?) throws {
guard let data = contents as? Data else {
fatalError("No file data")
}
guard let string = String(data: data, encoding: .utf8) else {
fatalError("Cannot load data into string")
}
csvData = try CSV(string: string)
}
}
我的自定义 Pivot 文档的第二个 UIDocument 子类如下。通过覆盖handleError() 函数,我可以看到保存失败,NSCocoaErrorDomain 出现错误,代码为513。
class PivotDocument: UIDocument {
var model: PivotModel!
var url: URL!
override func contents(forType typeName: String) throws -> Any {
let encoder = JSONEncoder()
return try encoder.encode(model)
}
override func load(fromContents contents: Any, ofType typeName: String?) throws {
guard let data = contents as? Data else {
fatalError("File contents are not Data")
}
let decoder = JSONDecoder()
model = try decoder.decode(PivotModel.self, from: data)
}
override func handleError(_ error: Error, userInteractionPermitted: Bool) {
let theError = error as NSError
print("\(theError.code)") // 513
print("\(theError.domain)") // NSCocoaErrorDomain
print("\(theError.localizedDescription)") // “example.pivot” couldn’t be moved because you don’t have permission to access “CSVs”.
super.handleError(error, userInteractionPermitted: userInteractionPermitted)
}
}
这在模拟器中有效(我的用户可以访问所有文件系统)但在 iOS 上无效(用户和应用程序权限不同)的事实让我认为我有权限问题。例如,我是否需要在我的 Xcode 项目中声明一些权利?
或者我只是滥用了UIDocument API,我需要寻找不同的实现吗?
【问题讨论】:
-
您无法写入应用沙箱之外的位置。您已经注意到模拟器没有沙盒,但您的应用程序是。
-
我是这么想的,但是其他应用是怎么做到的呢?我在 iWork 应用程序(例如 Numbers)和 iOS 的 Pixelmator 中注意到了这个功能。
标签: ios swift xcode icloud uidocument