gpt4 book ai didi

cocoa-touch - 无法让 UIDocumentBrowserviewController 在基于文档的应用程序中打开文档

转载 作者:行者123 更新时间:2023-12-02 21:51:23 26 4
gpt4 key购买 nike

我一直在开发一个新的基于文档的应用程序,并且对新的 UIDocumentBrowserController 感到非常高兴...尝试为文档浏览器 UI 推出我自己的解决方案很棘手!

创建文档后,我在让浏览器打开文档时遇到一些问题。

现在发生的情况是,当我选择在文档浏览器中创建新文档时,尽管会记录错误消息,但该文档会按预期创建并打开。但是,在文档关闭后,即使显示了文档,我也无法立即或随后启动时重新打开该文件。然而,这里的一个奇怪的线索是,如果我在创建文档后停止运行应用程序,但没有向其中添加新信息(触发保存周期),并再次运行项目,我可以正确打开文件。这让我认为文件的保存方式存在问题。(注意:在此阶段,我正在努力让本地非/icloud 实现正常工作,然后再继续 icloud 实现。)

以下是将文档保存到磁盘时(或至少在大多数情况下!)代码中任意位置出现的错误消息:2017-06-20 13:21:58.254938-0500 布道设计 2 iOS[22454:5000138] [默认] [错误] 无法获取项目文件的属性值:///Users/stevenhovater/Library/Developer/CoreSimulator/Devices/9A4364F2-B3A1-4AD9-B680-FB4BC876C707/data/Containers/Data/Application/DD534ED8-C4A3-40FE-9777-AED961976878/Documents/Untitled-9.sermon。错误:错误域 = NSFileProviderInternalErrorDomain 代码 = 1“不允许读者访问该 URL。” UserInfo={NSLocalizedDescription=读者无权访问该 URL。}

我怀疑问题出在我的文档类型 plist 中,我尝试通过模仿 wwdc 2017 session 229 视频中的设置来设置它。

enter image description here我的文档由 NSData 对象封装,使用我认为是 UIDocument 的相当标准的子类实现。 (我省略了生成缩略图的代码)

override func contents(forType typeName: String) throws -> Any {  

print("Saving Document Changes")
if sermon != nil {
let newData = NSKeyedArchiver.archivedData(withRootObject: sermon!)

return newData
} else {
let newData = NSKeyedArchiver.archivedData(withRootObject: Sermon())

return newData
}
}

override func fileAttributesToWrite(to url: URL, for saveOperation: UIDocumentSaveOperation) throws -> [AnyHashable : Any] {

let thumbnail:UIImage = self.createThumbnail()

let thumbnaildict = [URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey : thumbnail]
let dict = [URLResourceKey.thumbnailDictionaryKey:thumbnaildict]
return dict
}

override func load(fromContents contents: Any, ofType typeName: String?) throws {

guard let newSermon:Sermon = NSKeyedUnarchiver.unarchiveObject(with: contents as! Data) as? Sermon else{
throw documentErrors.invalidFile
}

self.sermon = newSermon

}

在我的 UIDocumentBrowserViewController 子类中,这是我用于获取本地文件名和创建新文档的代码。

func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) {  
var newDocumentURL: URL? = nil

print("creating new local document")

guard let target = self.newLocalFilename() else {
return
}
let targetSuffix = target.lastPathComponent
let tempURL = URL(fileURLWithPath: NSTemporaryDirectory() + targetSuffix)

let newDocument:SDDocument = SDDocument(fileURL: tempURL)

newDocument.sermon = Sermon()

/
newDocument.save(to: tempURL, for: .forCreating) { (saveSuccess) in

/
guard saveSuccess else {
/
importHandler(nil, .none)
return
}

/
newDocument.close(completionHandler: { (closeSuccess) in

/
guard closeSuccess else {
/
importHandler(nil, .none)
return
}

/
importHandler(tempURL, .move)
})
}

}

func newLocalFilename() -> URL? {
let fileManager = FileManager()

guard let baseURL = self.localDocumentsDirectoryURL.appendingPathComponent("Untitled")

else {return nil}

var target = baseURL.appendingPathExtension(DocumentBrowserViewController.documentExtension)

var nameSuffix = 2

while fileManager.fileExists(atPath: target.path) {
target = URL(fileURLWithPath: baseURL.path + "-\(nameSuffix).\(DocumentBrowserViewController.documentExtension)")

nameSuffix += 1
}
let targetSuffix = target.lastPathComponent
print("Target name: \(targetSuffix)")
print("new url: \(target)")

return target

}

最佳答案

经过四五个小时的努力解决这个问题,我发现了一个简单的解决方案:不要在模拟器中进行测试。我转而在我的设备上进行测试,一切立即开始按照广告宣传的那样工作。

[我无法从这里的经验中得出结论,但“在模拟器中不起作用”的问题可能仅限于 Sierra,但模拟器在 High Sierra 中确实有效。这可以解释为什么有些用户看到这个问题而其他用户却没有,尤其是为什么苹果在 WWDC 视频中似乎幸福地没有意识到这个问题。]

关于cocoa-touch - 无法让 UIDocumentBrowserviewController 在基于文档的应用程序中打开文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44665946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com