Iñi*_*tia 23 iphone nsfilemanager
我正在尝试在我的应用程序的/ 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);
}
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
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"}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?谢谢.
Ale*_*lds 50
如果您在错误域上搜索Google,NSCocoaErrorDomain则会发现该代码会513转换为错误NSFileWriteNoPermissionError.
这为您提供了解决此问题的关键线索:
这是包含应用程序本身的bundle目录.由于必须对应用程序进行签名,因此不得在运行时更改此目录的内容.这样做可能会阻止您的应用程序稍后启动.
具体来说,您无法修改已编译应用程序的包文件夹的内容.这是因为bundle是已编译的应用程序.
当您最终通过iTunes App Store分发应用程序时,该应用程序具有验证应用程序内容的数字签名.此签名在编译时生成.
如果您尝试在编译后更改捆绑包,则应用程序将更改并且数字签名不再有效.这使应用程序无效 - 谁知道那里有什么代码,对吧? - 最终用户将无法运行它.因此,如果您尝试修改捆绑包,Apple已设置iOS以引发错误.
相反,写入束,您的应用程序可以写一个三接受特定的应用程序文件夹:Documents,Temp和Cache.最有可能的是,您需要写入该Documents文件夹.
这些文件夹只能由您的应用访问.没有其他应用可以访问这些文件夹的内容.(同样,您的应用无法访问其他应用的文件夹.)
您可以设置应用程序,以允许最终用户通过桌面文件共享支持管理对iTunes文件数据的访问.
这是因为您不应该在运行时修改应用程序包.相反,您应该在其他位置添加资源.
编辑:
您看到的错误很可能是因为您无法写入捆绑包.
在使用Log库时,我遇到了同样的问题。最后是路径格式问题。检查dataPath格式。如果是Case 1,则有效。就我而言,它是Case 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
Run Code Online (Sandbox Code Playgroud)
如果dataPath有前缀,例如:file://,则无效。
对于 的实例NSURL,path将返回字符串 like case 1,并将absolutePath返回字符串 like case 2。