views:

122

answers:

3

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!

+2  A: 

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.....

iPhone Guy
You'd better check whether the file already exists in the Documents folder or not, so you copy it only when it doesn't exist yet.
JoostK
A: 

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];
Zoran Simic
A: 

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.

Kendall Helmstetter Gelner