Preloading documents into an iOS application

Situation : I have an iOS application that deals with files and allows the user to save, edit, open and perform various operations with these files. I would like to have some ready-made documents for the user to see when they open the application (for example, a template) along with their own documents.

Problem : How to create a document (or template file) and display it in the Documents folder after the user installs my application and launches it (and all the previous ones)?

Background : This document (the one to be installed in the application’s document directory) is created by me, not by the user.

I know that for this you need to save it in your package, and then, when your application is launched for the first time, quietly copy it to the document directory. Do I have to copy it in the appDidFinishLaunchingWithOptions method or in the viewDidLoad method and write the logic to determine if this was the first time I started the application?

My code : On this web page: http://textsnip.com/d35fbc But when it starts, it always says: “The file does not exist [in the folder with documents]”, then it tells me that it is being copied. The problem is that when I look at the application's document folder, it never exists, it is still in the package.

Why doesn't he copy this code How does it work?

+3
source share
2

, .

, .

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

, , , .

+5

, ( , ). , - :

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.
}
+3

All Articles