将 UIDocument 移动到文件系统上的新位置的正确方法是什么
我有一个基于文档的 iOS 应用程序,带有 UIDocument
子类。我需要能够移动它在文件系统上代表的文件。我考虑过用 NSFileManager 移动它,但这会弄乱 UIDocument 的 fileURL 属性。关于如何解决这个小难题有什么想法吗?
I have a document-based iOS application with a UIDocument
subclass. I need to be able to move the file it represents on the file-system. I have considered moving it with an NSFileManager
, but this would mess up the UIDocument
's fileURL
property. Any ideas on how to solve this little conundrum?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(3)
这可能很旧但仍然相关。
您想要执行的操作如下:
对于移动:使用 NSFileCoordinator 移动文件,在协调器的块内,调用
对于删除:覆盖您的 UIDocument 子类或实现文件呈现器协议方法
accommodatePresentedItemDeletionWithCompletionHandler:
来关闭文档。从而确保它能够正确处理移动。
This might be old but still relevant.
What you want to do is the following:
For moving: move your file using NSFileCoordinator, inside the coordinator's block, call
For deleting: overwrite your UIDocument subclass or implement the file presenter protocol method
accommodatePresentedItemDeletionWithCompletionHandler:
to close the document.Thus ensuring it will correctly handle being moved.
您可以通过首先关闭 UIDocument,然后在完成处理程序中使用 NSFileManager 移动文件来重命名它。成功移动文件后,使用新文件 URL 初始化 UIDocument 子类的新实例:
You can rename a UIDocument by first closing it, and then in the completion handler, moving the file using NSFileManager. Once the file has been successfully moved, initialize a new instance of your UIDocument subclass using the new file URL:
我在
UIDocument
类规范 (5.1) 中找到了这一点:我也在绝望地寻找,希望以上内容有所帮助。我还没有测试过——我现在就开始尝试。
所以基本上,如果我没有错过这里的任何内容,您必须使用
NSFileManager
移动文档,然后在文档上调用presentedItemDidMoveToURL:
。您可能必须使用 NSFileCoordinator 移动文件,以确保不会出现问题。请纠正这个答案中所有错误的地方。在所有这些事情上我仍然是一个n00b。
I found this in the
UIDocument
class specification (5.1):I was hopelessly searching too and I hope the above helps. I haven't tested it yet — I'm jumping on it right now.
So basically, if I don't miss anything here, you have to move the document using
NSFileManager
and then callpresentedItemDidMoveToURL:
on your document. You have probably to move the file usingNSFileCoordinator
, to make sure you don't get problems.Please correct everything in this answer that's wrong. I'm still a n00b in all that stuff.