将文档预装到iOS应用程序中 [英] Preloading Documents into iOS App

查看:107
本文介绍了将文档预装到iOS应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:
我有一个iOS应用程序处理文件,并允许用户使用这些文件保存,编辑,打开和执行各种操作。我希望能够为用户提供一些预制的文档,以便在用户打开应用程序(例如模板)的同时查看自己的自定义文档。



<问题:
如何创建一个文档(或模板文件)并在用户安装我的应用程序并启动它(以及所有前面的时间)之后让它出现在Documents文件夹中, ?

背景
这个文档(将被安装到应用程序的文档目录中)是由我创建的,而不是用户。



我知道要做到这一点,您需要将其保存在您的包中,然后当您的应用第一次运行时,将其默认复制到文档目录。我应该将其复制到appDidFinishLaunchingWithOptions方法或我的viewDidLoad方法中,并编写逻辑来检测它是否是第一次运行应用程序?

我的代码 strong>:
在这个网页: http://textsnip.com/d35fbc
但是当它运行时,它总是说:文件不存在[在文档文件夹],然后它告诉我,它正在被复制。问题是,当我检查应用程序的文档文件夹,它永远不会在那里,它仍然在捆绑。

为什么不复制这个代码
这是如何工作的?

解决方案

这些文件实际上必须添加到应用程序包中,然后在应用程序启动时以静默方式复制。



下面的代码将一个文本文件从我的包的顶层复制到documents目录中(如果它尚不存在的话)。

  NSFileManager * fileManager = [NSFileManager defaultManager]; 
NSError *错误;
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * documentsDirectory = [paths objectAtIndex:0];

NSString * txtPath = [documentsDirectory stringByAppendingPathComponent:@txtFile.txt];

if([fileManager fileExistsAtPath:txtPath] == NO){
NSString * resourcePath = [[NSBundle mainBundle] pathForResource:@txtFileofType:@txt];
[fileManager copyItemAtPath:resourcePath toPath:txtPath error& error];





这个代码的唯一缺陷就是如果用户删除了这个文件,立即重新出现,当他们再次打开应用程序。


The Situation: I have an iOS app that deals with files and lets the user save, edit, open and perform various operations with these files. I'd like to be able to have some pre-made documents for the user to look at when they open the app (ex. a template) alongside their own custom documents.

The Problem: How can I create a document (or template file) and have it appear in the Documents folder after the user installs my app and launches it (and all preceding times)?

Background: This document (the one that'd be installed into the app's documents directory) is created by me, not the user.

I know that to do this you need to save it in your bundle, and then when your app runs for the first time silently copy it into the Documents Directory. Should I copy it in the appDidFinishLaunchingWithOptions method or in my viewDidLoad method and write logic to detect if it's the first time the app has run?

My Code: At this webpage: http://textsnip.com/d35fbc     But when it runs, it always says: "File does not exist [in documents folder]", then it tells me that it's being copied. The problem is that when I examine the app's documents folder it is never there, it's still in the bundle.

Why won't it copy with this code How does this work?

解决方案

the files must in fact be added to the app bundle, then copied silently when the app launches.

The following code copies a text file from the top level of my bundle to the documents directory if it doesn't already exist.

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"txtFile.txt"];

if ([fileManager fileExistsAtPath:txtPath] == NO) {
    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"txtFile" ofType:@"txt"];
    [fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
}

The only flaw in this code is that if the user deletes the file, it immediately reappears when they open the app again.

这篇关于将文档预装到iOS应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆