English 中文(简体)
预先将文档装入 iOS App
原标题:Preloading Documents into iOS App

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.

我知道要做到这一点,你需要将它保存在包中,然后当你的应用程序第一次悄悄地复制到文档目录中。我是否应该把它复制到 Appdudie Finish Launching withOptions 方法中,或者在我看来,是否Load 方法并写逻辑来检测它是否第一次运行?

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?

最佳回答

文件实际上必须添加到应用程序包中, 然后在程序启动时悄悄复制 。

如果文档目录不存在的话, 以下代码会复制从我包的顶层到文档目录的文本文件 。

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

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

问题回答

正如你说的,你把它包括在你的应用程序包中(把它添加到您的项目中,确保它成为你目标的一部分)。然后,你可以通过这样称呼它来访问它路径:

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"MyTemplateDoc" 
                                                       ofType:@"extension"];

然后复制到应用程序文件文件夹 。

NSString *docPath = <a path in your documents folder>
NSError *error = nil;
[[NSFileManager defaultManager] copyItemAtPath:bundlePath 
                                        toPath:docPath 
                                         error:&error];
if (error) {
    // handle copy error.
}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签