将文件复制到iOS中的其他目录

时间:2013-02-18 09:24:57

标签: ios objective-c cocoa-touch nsfilemanager

我想将文件从app资源复制到文档目录。我正在使用此代码,但这会将文件复制到我自己的应用程序文档目录中。

NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *storePath = [documentsDirectory stringByAppendingPathComponent: @"TestFile.txt"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"TestFile" ofType:@"txt"];

[[NSFileManager defaultManager] copyItemAtPath:filePath toPath:storePath error:nil];
if (![fileManager fileExistsAtPath:getDatabasePath])  {
    NSLog(@"file doesn't exist");
    // file doesn't exist
} else {
     NSLog(@"file exist");
}

我试图将此代码更改为,但它不起作用,也许就是这样的

NSString *storePath = [documentsDirectory stringByAppendingPathComponent: @"Applications/AppFolder/Documents/TestFile.txt"];

那么有没有办法将文件复制到不同的目录?

1 个答案:

答案 0 :(得分:2)

您无法从任何应用程序内部将一个应用程序的资源复制到其他应用程序,因为您不会获得对其他应用程序的任何引用。每个应用程序都有自动生成的应用程序ID,您无法从其他应用程序访问它。

如果你想在设备中实现它,那是不可能的,但是如果你在模拟器中尝试某些东西,只需手动复制粘贴资源..