我在 Xcode 中创建了一个应用程序,并添加了一个文本文件供该应用程序打开和显示。
如何将此文本文件放入 iPad 沙箱文档目录进行测试?
最佳答案
有几件事你需要做:
- 创建您的文档目录的路径
- 确保您的文本文件在您的应用程序包中(拖放到 Xcode 中应该没问题)
- 按名称将该文本文件从应用程序包复制到 Documents 目录
试试这个:
// Documents path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
// Destination path
NSString *fileInDocumentsPath = [documentsPath stringByAppendingPathComponent:@"name-of-your-file.txt"];
// Origin path
NSString *fileInBundlePath = [[NSBundle mainBundle] pathForResource:@"name-of-your-file" ofType:@"txt"];
// File manager for copying
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:fileInBundlePath toPath:fileInDocumentsPath error:&error];
关于xcode - 将数据文件从 Xcode 复制到 iPad 沙箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15843870/