iPhone - 将文件从资源复制到Documents会产生错误

los*_*sit 8 iphone

我正在尝试将我的Resources文件夹中的mp3文件复制到应用程序"Documents"文件夹中的文件夹.在模拟器上这很好用.但是当我在设备上运行它时,复制文件会给我带来这个错误

Operation could not be completed. (Cocoa error 513.)
Run Code Online (Sandbox Code Playgroud)

源和目标路径很好,但我仍然无法复制该文件.有任何想法吗?我在哪里可以找到可可错误代码513的含义?

谢谢.

这是相关的源代码

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"Files"];

    NSString *insPath = [NSString stringWithFormat:@"%@.mp3", fileName];
    NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:insPath];
    NSString *destPath = [folderPath stringByAppendingPathComponent:insPath];
    NSLog(@"Src: %@, Dest: %@", srcPath, destPath);

    NSError *err;
    [fileManager moveItemAtPath:srcPath toPath:destPath error:&err];

    NSLog(@"Err desc-%@", [err localizedDescription]);
    NSLog(@"Err reason-%@", [err localizedFailureReason]);
Run Code Online (Sandbox Code Playgroud)

在调用moveItemAtPath之前,我还创建了目录"Files",它返回YES.

这是日志结果

Src: /var/mobile/Applications/512D7565-7EF7-4C13-A015-19EEC3F3B465/MyApp.app/MyFile.mp3, Dest: /var/mobile/Applications/512D7565-7EF7-4C13-A015-19EEC3F3B465/Documents/Files/MyFile.mp3

Err desc-Operation could not be completed. (Cocoa error 513.)
Err reason-(null)
Run Code Online (Sandbox Code Playgroud)

一个问题

将数据从资源复制到Documents文件夹时,文件大小是否有限制?我试图复制的文件大约是5MB.这可能是个原因吗?

小智 19

编辑:

刚刚找到了一个更简单的解决方案.而不是moveItemAtPath:toPath:error:,只需使用copyItemAtPath:toPath:error:...因为我们真的想从mainBundle复制文件而不是移动它.我应该早点想到这个!

例如

[[NSFileManager defaultManager] copyItemAtPath:mainBundleFilePath 
                                        toPath:destPath 
                                         error:&err]
Run Code Online (Sandbox Code Playgroud)

请参阅我之前的评论,了解其原因.


我相信我有这个问题的答案.我可以肯定地说问题不是目标文件路径.

我得到了相同的Cocoa错误513(NSFileWriteNoPermissionError),几乎完全相同的代码:

[[NSFileManager defaultManager] moveItemAtPath:mainBundleFilePath 
                                        toPath:destPath 
                                         error:&err]
Run Code Online (Sandbox Code Playgroud)

问题似乎是来自mainBundle的文件没有适当的权限移动到另一个地方.我不确定这个命令,如果执行,是否会实际从mainBundle 移动文件或只是复制它...但无论如何,文件管理器似乎不喜欢这个想法.

解决方案很简单:只需将mainBundle文件读入NSData对象,然后将NSData写入新文件即可.请注意,两个示例中的目标文件路径都相同,这表明lostInTransit说他的文件路径正常是正确的.

对于此示例,以下代码将起作用而不会引发错误:

NSData *mainBundleFile = [NSData dataWithContentsOfFile:mainBundleFilePath];
[[NSFileManager defaultManager] createFileAtPath:destPath 
                                        contents:mainBundleFile 
                                      attributes:nil];
Run Code Online (Sandbox Code Playgroud)

顺便说一句,在我自己的代码中,我没有为属性传递nil,而是设置了一个带有NSFileModificationDate属性的NSDictionary.我还在处理if语句的错误中包装了createFileAtPath:contents:属性.换一种说法,

if (![[NSFileManager defaultManager] createFileAtPath:destPath 
                                             contents:mainBundleFile 
                                           attributes:myAttributes]) { 
 // handle error as necessary, etc...
Run Code Online (Sandbox Code Playgroud)

}

我花了一段时间来计算所有这些,所以希望这个解决方案对其他人有所帮助.