1

My app has created several data files as development has progressed through the simulator. Their location is obtained by this function:

NSString *pathInDocumentDirectory(NSString *fileName) {
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                                       NSUserDomainMask, 
                                                                       YES);    
    NSString *documentDirectory = [documentDirectories objectAtIndex: 0];
    return [documentDirectory stringByAppendingPathComponent: fileName];  
}

The files are now required on the devices as testing of the app is moving from the simulator to actual devices. I was under the impression that the location that I am saving these files to made them part of the bundle and that therefore they would be on the device at the point of the build. Apparently that is not the case as the files do not exist on the device, but are present in directories on my development machine. How do I transfer the data files from my current working environment to the devices? How do I make these files get transferred from the simulator to the device automatically as they are written by the program?

StoneBreaker
  • 723
  • 8
  • 19
  • 1
    You're going to have to add these files to your bundle and copy them to your application's document directory upon first launching it. – Rog Jun 07 '12 at 22:47
  • @Rog OK, Thanks. I have added them to my bundle by dragging and dropping them (with the copy box checked). I am not sure how to programmatically copy them from where they are to the applications document directory upon first launching. (Where are they? - I have them in their own group in the source but all of that is in a single flat dir I don't see in the bundle on the device.) What methods do I use to copy files around the bundle once I find the files to copy? – StoneBreaker Jun 07 '12 at 23:01
  • 2
    To get the path to a file in the main bundle [[NSBundle mainBundle] pathForResource:@"file name"]. You already know how to get the path to the documents directory, now check the docs for NSFileManager and you have everything you need. – Rog Jun 07 '12 at 23:15

1 Answers1

1
  • You could enable iTunes File Sharing in your app which would enable you to add files to the Documents directory from iTunes.

    All you need to do is add the boolean key UIFileSharingEnabledto your Info.plist and set it to true or YES (make sure you disable this before submitting if you don't want users to have this access). For more information, check out this tutorial.

OR

  • You could use PhoneDisk to copy the files into your app's Documents directory.
jin
  • 725
  • 3
  • 10