在mainBundle中的资源Path中创建文件时是否有任何权限

时间:2012-01-17 21:19:19

标签: iphone file file-io permissions

我希望能够从resourcePath创建,重命名或删除mainBundle内的临时文件。

NSString* path = [[NSBundle mainBundle] resourcePath];
NSFileManager* filemgr = [NSFileManager defaultManager];

for (int i=0; i<size; i++)
{
    NSString* file = [NSString stringWithFormat:@"%@/_%i.jpg", path, i];

    if ([filemgr fileExistsAtPath:file] == YES)
    {
        [filemgr removeItemAtPath:file error: NULL];
    }

    [file release];
}

[filemgr release];

我是否需要额外的权限才能在真实设备上运行?

2 个答案:

答案 0 :(得分:2)

无法在设备上的主束资源目录中创建/重命名/删除任何内容。在模拟器上它似乎工作。常见的解决方案是将资源从捆绑包复制到应用程序的文档目录,然后对副本进行操作。

这是你获取文件目录的方式:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
之前曾问过{p> Similar question

答案 1 :(得分:0)

您可以在项目资源文件夹中的模拟器生成的文件中检测模拟器或设备,并从设备中获取资源路径。