在沙盒应用程序中使用 NSSavePanel 替换文件 [英] Replace a file using the NSSavePanel in a sandboxed application

查看:97
本文介绍了在沙盒应用程序中使用 NSSavePanel 替换文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码创建了一个 NSSavePanel:

I create a NSSavePanel using this code:

NSSavePanel *savePanel = [NSSavePanel savePanel];
savePanel.delegate = self;

savePanel.directoryURL = ...;
savePanel.nameFieldStringValue = ...;

[savePanel beginSheetModalForWindow:self.window
                  completionHandler:^(NSInteger returnCode) {

if (returnCode == NSFileHandlingPanelOKButton) {

// the completion handler

}
}];

如果用户在保存面板中选择了一个已经存在的文件,则会出现警告框XXX"已经存在.您要替换它吗?".

If in the save panel the user select an already existing file, appears the alert box ""XXX" already exists. Do you want to replace it?".

如果用户按下替换"按钮,则在完成处理程序中,使用 NSFileManagerremoveItemAtPath:error: 方法删除旧文件,然后删除新文件文件已创建(实际上:它是在临时位置创建的,然后使用 moveItemAtPath:toPath:error: 方法移动,但我认为这只是一个实现细节):

If the user press the "Replace" button, in the completion handler the old file is removed with the removeItemAtPath:error: method of NSFileManager, and then the new file is created (in reality: it is created in a temporary location and then moved using moveItemAtPath:toPath:error: method, but I think this is just an implementation detail):

if (returnCode == NSFileHandlingPanelOKButton) {
  // overwrite the url, because if we are here is because the user has already
  // expressed its willingness to overwrite the previous file
  NSError *error = nil;
  BOOL res = [[NSFileManager defaultManager] removeItemAtURL:savePanel.URL error:&error];

  if (res) {
    res = [[NSFileManager defaultManager] moveItemAtPath:tmpFilePath toPath:savePanel.URL error:&error];
  }

  // ...
}

过去,一切都正常.然而,今天,我开始使用具有读/写访问"权限的 Lion's Sandbox.

In the past, everything has always worked properly. Today, however, I started to use the Lion's Sandbox with the "Read/Write Access" entitlement.

使用沙箱,removeItemAtPath:error: 成功,但下面的moveItemAtPath:toPath:error: 返回错误.

With the sandbox, the removeItemAtPath:error: is successful, but the following moveItemAtPath:toPath:error: returns an error.

这似乎是合理的,因为 Powerbox 赋予我访问(读取和写入)文件的权限.当我删除此文件时,授予我的权利已用尽.

It seems reasonable, because Powerbox gives me the rights to access (in reading and writing) a file. When I remove this file, the right granted to me is exhausted.

我猜对了吗?

我该如何解决这个问题?

How can I solve this problem?

推荐答案

问题是,一旦您删除了该文件,您对该文件的权限也将消失.您需要做的是覆盖文件,例如使用 [[NSFileManager defaultManager] createFileAtPath:contents:attributes:].NSFileManager 文档 说明此方法的以下内容:

The problem is that once you delete the file, your rights on that file also vanish. What you need to do instead is overwrite the file, for example using [[NSFileManager defaultManager] createFileAtPath:contents:attributes:]. The NSFileManager documentation states the following for this method:

如果文件已存在于路径中,并且当前进程具有相应权限,则此方法将覆盖该文件的内容.

If a file already exists at path, this method overwrites the contents of that file if the current process has the appropriate privileges to do so.

使用来自 NSData/NSMutableData 的方法也可能会有所帮助.

Using methods from NSData/NSMutableData might also be helpful.

这篇关于在沙盒应用程序中使用 NSSavePanel 替换文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆