1

将 App 安装到 iPhone 时,有没有办法将文件安装到 App 的 Documents 文件夹中?

换一种说法:当用户下载一个应用程序并将其安装在他们的 iPhone 上时,我想自动将一些文件安装到应用程序的 Documents 目录中。

例如:我有一个文件 foo.txt,我在开发时为我的 App SeeFooRun 创建。当应用程序安装时,我希望 foo.txt 出现在 Documents 目录中,这样当我第一次运行应用程序时,我会从 Documents 目录而不是从 App Bundle 访问 foo.txt。

谢谢!

在示例中,当我想说“安装”时,我说“第一次运行”,然后我更改了句子的其余部分以适应。很抱歉混淆了!

4

3 回答 3

2

我会使用这种方法来做到这一点:

在根 plist 中创建一个属性,将初始值设置为“NO” 当应用程序运行时,检查此值。

如果值为“NO”,则在文档目录中创建文档,将值更改为“YES”并保存该值。

下次运行应用程序时,该值将为“YES”,并且不会重建文件。

希望这可以帮助.....

于 2009-12-25T19:18:23.667 回答
0

复制到 Documents 答案的一个细微变化 - 将您复制的所有内容放入您创建的 Documents 中的一个文件夹中,然后在启动时您只需检查该文件夹是否存在,然后进行所有复制。

但是,检查每个文件更加健壮,特别是如果您在更新中添加新文件,那么复制逻辑将复制到单个新文件中,而不会覆盖旧文件。

于 2009-12-25T21:18:06.500 回答
0

你可以这样做:

NSFileManager *fileManager = [NSFileManager defaultManager];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *pathLocal = [documentsDir stringByAppendingPathComponent:@"foo.txt"];

NSString *pathBundle = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"foo.txt"];

NSError *error;
BOOL success = [fileManager copyItemAtPath:pathBundle toPath:pathLocal error:&error];
于 2009-12-25T19:25:12.977 回答