在iOS应用中,是否有可能将新文件写入捆绑资源目录?

9
在iOS应用程序中,是否有可能将新文件写入捆绑资源目录?
2个回答

14

无法在束目录中编写文件,因为该目录已用SSL证书进行代码签名,无法打破它。但是,您可以轻松地在iPhone应用程序的文档目录中编写文件。

您可以使用以下方法获取文档目录路径 -

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

2

或者您可以复制所需资源,然后在具有写入权限的文件上进行操作。

NSArray  *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDir = dirPaths[0];

NSString *databasePath = [documentsDir stringByAppendingString:@"/TwigitDatabase.db"];

NSFileManager *fm = [NSFileManager defaultManager];

if(![fm fileExistsAtPath:databasePath]) {   // Checking whether the my database was copied earlier, though it could be saved in some preferences property.
    NSError *error;
        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"TwigitDatabase" ofType:@"db"];
    [[NSFileManager defaultManager] copyItemAtPath:soundFilePath toPath:databasePath error:&error];

    NSLog(@"%@", error);


}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接