【问题标题】:Copy File to different directory in iOS将文件复制到iOS中的不同目录
【发布时间】:2013-02-02 16:30:01
【问题描述】:

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

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"];

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

【问题讨论】:

  • 我认为你不能访问根目录的文件。这就是在沙盒中拥有应用程序的好处。
  • 抱歉,这是 HomeDirectory 不是 rootDirectory。

标签: ios objective-c cocoa-touch nsfilemanager


【解决方案1】:

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

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

【讨论】:

  • 是的,我想在设备上怎么做?您能向我建议在设备上执行此操作的可能方式吗?谢谢。
  • 您无法在设备中实现,也无法在模拟器中实现,只能通过 cp 之类的终端命令来实现(但仍然需要越狱设备),但您仍然必须确定确切的应用程序文件夹那是不可能的..
  • 我相信有一个人可以做到这一点,就像许多越狱应用程序可以覆盖任何应用程序文件夹中的保存游戏读/写 plist 一样,是的,我们总是在越狱设备上。所以我想知道我怎样才能实现这样的应用程序?
猜你喜欢
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
相关资源
最近更新 更多