在iPhone上安装应用程序时,是否可以将文件安装到应用程序的Documents文件夹中?

发布于 2024-08-15 19:11:24 字数 351 浏览 2 评论 0原文

在iPhone上安装应用程序时,是否可以将文件安装到应用程序的Documents文件夹中?

换句话说:当用户下载一个App并将其安装到他们的iPhone上时,我想自动安装一些文件到App的Documents目录中。

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

谢谢!

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

When installing an App to the iPhone, is there a way to install files to the App's Documents folder?

To say it another way: When the user downloads an App and installs it on their iPhone, I want to automatically install some files to the App's Documents directory.

For example: I have a file foo.txt that I create at development time for my App SeeFooRun. When the App installs, I want foo.txt to appear in the Documents directory so that when I run the App for the first time I access foo.txt from the Documents directory instead of from the App Bundle.

Thanks!

In the example I said "runs for the first time" when I meant to say "installs" and I changed the rest of the sentence to fit. Sorry for the mix up!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱暴击 2024-08-22 19:11:24

我会使用以下方法来完成此操作:

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

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

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

希望这有帮助......

I would do it using this method:

Create a property in the root plist set the initial value to "NO" When the app is run, check this value.

If the value is "NO", create the document in the document directory, change the value to "YES" and save the value.

The next time the app is run, the value will be "YES" and the file won't be rebuilt.

Hope this helps.....

谜兔 2024-08-22 19:11:24

你可以这样做:

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

You can do something like this:

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];
樱花细雨 2024-08-22 19:11:24

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

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

One slight variation on the copying into Documents answers - put everything you copy into a folder in Documents that you create, then at startup you simply check for the presence of that one folder and do all the copying then.

Checking per file is more robust though, especially if you add a new file in an update then the copy logic will copy in the single new file without overwriting the older ones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文