gpt4 book ai didi

ios - 从 MSOffice 应用程序保存新文档时,NSFileProvider importDocument 提供空文件的 fileURL

转载 作者:可可西里 更新时间:2023-11-01 01:08:54 28 4
gpt4 key购买 nike

我正在尝试在 Word.app 中创建一个新文档并通过 FileProvider 扩展保存到我的应用程序。我的适当方法的实现是:

    override func importDocument(at fileURL: URL,
toParentItemIdentifier parentItemIdentifier: NSFileProviderItemIdentifier,
completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void)
{

let internalUrl = NSFileProviderManager.default.documentStorageURL.appendingPathComponent(fileURL.lastPathComponent, isDirectory: false)

guard fileURL.startAccessingSecurityScopedResource() else { fatalError() }
try! FileManager.default.copyItem(at: fileURL, to: internalUrl) // breakpoint here
fileURL.stopAccessingSecurityScopedResource()

// update local db, whatever

completionHandler(TemporaryItem(forImporting: internalUrl, into: parentItemIdentifier), nil)
}

显然,当我通过 po FileManager.default.attributesOfItem(forPath: fileURL.path) 命令放置断点并检查文件属性时,NSFileSize 的值为 0。命令 po FileManager.default.contents(atPath: fileURL.path) 返回 0 字节数据和 0x000000000000bad0 指针。写入 internalUrl 的文件也是空的。

最奇怪的是,这种情况只发生在 MS Word、Excel 和 PowerPoint 应用程序中。从 PSPDFKit、文件或照片保存的文件的相同代码完美运行。另一方面,Word 正确地将文件保存到其他文件提供程序(如 Dropbox),因此问题不应该存在。

我曾尝试使用文件协调器来做到这一点,但这没有帮助。我已经验证每个 startAccessingSecurityScopedResource() 都有它的 stopAccessingSecurityScopedResource()。我在两台 iOS11.3 设备上测试过 - 相同的行为。我什至找到了其他 open source执行相同操作的应用程序。

除了期望 iOS 应用程序扩展能够工作之外,我做错了什么?

最佳答案

因为Word应用会触发多次importDocument...

首先调用 importDocument,它会尝试在文件提供程序扩展名上创建空文件。这就是导入文件大小为 0 的原因。

如果处理得当,Word 应用程序将获取保存的文件路径并更新其上的文件。然后它会触发下一个 itemChangedAtURL: api 与它刚刚得到的文件路径。

关于ios - 从 MSOffice 应用程序保存新文档时,NSFileProvider importDocument 提供空文件的 fileURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50131487/

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