NSFileManager 创建文件夹(Cocoa 错误 513。)
我正在尝试在我的应用程序的 /sounds 文件夹中创建一个文件夹。
-(void)productPurchased:(UAProduct*) product {
NSLog(@"[StoreFrontDelegate] Purchased: %@ -- %@", product.productIdentifier, product.title);
NSFileManager *manager = [NSFileManager defaultManager];
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSError *error;
NSString *dataPath = [NSString stringWithFormat:@"%@/sounds/%@", bundleRoot, product.title];
if (![manager fileExistsAtPath:dataPath isDirectory:YES]) {
[manager createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&error];
NSLog(@"Creating folder");
}
NSLog(@"%@", error);
}
但我收到此错误:
Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x175120 {NSFilePath=/var/mobile/Applications/D83FDFF9-2600-4056-9047-05F82633A2E4/App.app/sounds/Test Tones, NSUnderlyingError=0x117520 "The operation couldn’t be completed. Operation not permitted"}
我做错了什么? 谢谢。
I'm trying to create a folder inside the /sounds folder of my app.
-(void)productPurchased:(UAProduct*) product {
NSLog(@"[StoreFrontDelegate] Purchased: %@ -- %@", product.productIdentifier, product.title);
NSFileManager *manager = [NSFileManager defaultManager];
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSError *error;
NSString *dataPath = [NSString stringWithFormat:@"%@/sounds/%@", bundleRoot, product.title];
if (![manager fileExistsAtPath:dataPath isDirectory:YES]) {
[manager createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&error];
NSLog(@"Creating folder");
}
NSLog(@"%@", error);
}
But I get this error:
Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x175120 {NSFilePath=/var/mobile/Applications/D83FDFF9-2600-4056-9047-05F82633A2E4/App.app/sounds/Test Tones, NSUnderlyingError=0x117520 "The operation couldn’t be completed. Operation not permitted"}
What am I doing wrong?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您在错误域上搜索 Google
NSCocoaErrorDomain
您发现代码513
转换为错误NSFileWriteNoPermissionError< /代码>
。
这为您提供了解决此问题的关键线索:
具体来说,您无法修改已编译应用程序的捆绑文件夹的内容。这是因为捆绑包是已编译的应用程序。
当您最终通过 iTunes App Store 分发应用程序时,该应用程序将具有验证应用程序内容的数字签名。该签名是在编译时生成的。
如果您尝试在编译后更改捆绑包,则应用程序会发生更改并且数字签名不再有效。这会使应用程序失效——谁知道里面有什么代码,对吧? - 因此,Apple 设置了 iOS,如果您尝试修改应用程序,则会抛出错误。
您的应用程序可以写入三个接受的特定于应用程序之一,而不是写入捆绑包文件夹:
/Documents
、/tmp
和/Library/Caches
。最有可能的是,您需要写入
/Documents
文件夹。这些文件夹只能由您的应用程序访问。没有其他应用程序可以访问这些文件夹的内容。 (同样,您的应用程序无法访问其他应用程序的文件夹。)
您可以将您的应用程序设置为允许最终用户通过 iTunes 管理对文件数据的访问,通过 桌面文件共享支持。
If you search Google on the error domain
NSCocoaErrorDomain
you find that the code513
translates to the errorNSFileWriteNoPermissionError
.This provides you with the critical clue for solving this problem:
Specifically, you cannot modify the contents of a compiled app's bundle folder. This is because the bundle is the compiled application.
When you eventually distribute the app through the iTunes App Store, the application has a digital signature that validates the contents of the app. This signature is generated at compile time.
If you try to change the bundle after compilation, the app changes and the digital signature is no longer valid. This invalidates the application — who knows what code is in there, right? — and so Apple has set up iOS to throw an error if you try to modify the application.
Instead of writing to the bundle, your app can write to one of three accepted app-specific folders:
<Application_Home>/Documents
,<Application_Home>/tmp
and<Application_Home>/Library/Caches
.Most likely, you will want to write to the
<Application_Home>/Documents
folder.These folders are only accessible to your app. No other app can access the contents of these folders. (Likewise, your app cannot access another app's folders.)
You can set up your app to allow the end user to manage access to file data through iTunes, via desktop file sharing support.
这是因为您永远不应该在运行时修改应用程序的包。相反,您应该在其他地方有一个可以添加资源的文件夹。
编辑:
您看到的错误很可能是因为您无法写入捆绑包。
This is because you should never modify the bundle of your application at runtime. Instead, you should have a folder elsewhere where you can add resources.
EDIT:
The error you are seeing is most likely because you cannot write to the bundle.
使用 Log 库时,我遇到了同样的问题。最后是路径格式问题。检查
dataPath
格式。如果是情况1
,则有效。就我而言,这是情况2
,所以我无法创建目录。如果
dataPath
有前缀,例如:file://
,则它无效。对于
NSURL
的实例,path
将返回类似case 1
的字符串,而absolutePath
将返回该字符串就像案例2
。I encounter the same problem, when using a Log library. Finally, it's path format problem. Check the
dataPath
format. If it isCase 1
, it is valid. In my case, it'sCase 2
, so I failed to create directory.If the
dataPath
has a prefix, ex:file://
, it is invalid.As for an instance of
NSURL
,path
will return the string likecase 1
, andabsolutePath
will return the string likecase 2
.在我的例子中,我仍然不完全清楚 513 错误的含义,但是当我尝试使用
[NSFileHandle fileHandleForReadingFromURL:theUrl error:&err ]
。我从这个答案中意识到,使用iOS 13,我现在需要使用
startAccessingSecurityScopedResource
来访问外部在应用程序中打开的文件。当我按如下方式包装文件调用时,错误 513 不再发生:I'm still not totally clear on the meaning of the 513 error in my case, but I was getting it when just trying to read an opened file URL using
[NSFileHandle fileHandleForReadingFromURL:theUrl error:&err ]
.I realized from this answer that with iOS 13, I now need to use
startAccessingSecurityScopedResource
to access external files that are opened in the app. When I wrapped my file calls as follows, then the error 513 stopped occurring: