NSFileManager 创建文件夹(Cocoa 错误 513。)

发布于 2024-09-13 16:11:41 字数 1076 浏览 3 评论 0原文

我正在尝试在我的应用程序的 /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

为你拒绝所有暧昧 2024-09-20 16:11:41

如果您在错误域上搜索 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 code 513 translates to the error NSFileWriteNoPermissionError.

This provides you with the critical clue for solving this problem:

This is the bundle directory containing the application itself. Because an application must be signed, you must not make changes to the contents of this directory at runtime. Doing so may prevent your application from launching later.

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.

心是晴朗的。 2024-09-20 16:11:41

这是因为您永远不应该在运行时修改应用程序的包。相反,您应该在其他地方有一个可以添加资源的文件夹。

编辑:
您看到的错误很可能是因为您无法写入捆绑包。

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.

┊风居住的梦幻卍 2024-09-20 16:11:41

使用 Log 库时,我遇到了同样的问题。最后是路径格式问题。检查 dataPath 格式。如果是情况1,则有效。就我而言,这是情况2,所以我无法创建目录。

// Case 1
/var/mobile/Containers/Data/Application/5FB2CD2D-91DC-4FB2-8D6F-06369C70BB4A/Library/Caches/AppLogs

// Case 2, invalid format
file://var/mobile/Containers/Data/Application/5FB2CD2D-91DC-4FB2-8D6F-06369C70BB4A/Library/Caches/AppLogs

如果 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 is Case 1, it is valid. In my case, it's Case 2, so I failed to create directory.

// Case 1
/var/mobile/Containers/Data/Application/5FB2CD2D-91DC-4FB2-8D6F-06369C70BB4A/Library/Caches/AppLogs

// Case 2, invalid format
file://var/mobile/Containers/Data/Application/5FB2CD2D-91DC-4FB2-8D6F-06369C70BB4A/Library/Caches/AppLogs

If the dataPath has a prefix, ex: file://, it is invalid.


As for an instance of NSURL, path will return the string like case 1, and absolutePath will return the string like case 2.

素染倾城色 2024-09-20 16:11:41

在我的例子中,我仍然不完全清楚 513 错误的含义,但是当我尝试使用 [NSFileHandle fileHandleForReadingFromURL:theUrl error:&err ]

我从这个答案中意识到,使用iOS 13,我现在需要使用startAccessingSecurityScopedResource来访问外部在应用程序中打开的文件。当我按如下方式包装文件调用时,错误 513 不再发生:

if( [myURL startAccessingSecurityScopedResource] ) 
{
    NSFileHandle* myFile = [NSFileHandle fileHandleForReadingFromURL:myURL error:&err ];
    // ...Do file reads here...
    [theUrl stopAccessingSecurityScopedResource];
}

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:

if( [myURL startAccessingSecurityScopedResource] ) 
{
    NSFileHandle* myFile = [NSFileHandle fileHandleForReadingFromURL:myURL error:&err ];
    // ...Do file reads here...
    [theUrl stopAccessingSecurityScopedResource];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文