5

I'm having a really weird problem with the iPad app I'm writing. On startup I want to copy a folder containing a few other folders (that are empty) from the application bundle to the Documents directory. The folder to be copied, called 'flds' (all lowercase), was added to the Xcode 4 project using 'Create folder references for any added folders', and I have checked that it is actually part of the .app file after compiling. (And that it doesn't exist already when attempting to copy.)

I've tried getting to the 'flds' folder using either one of these calls (they all work):

[[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"flds"]
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"flds"]
[[NSBundle mainBundle] pathForResource:@"flds" ofType:@""]

Then copying using NSFileManager's copyItemAtPath:toPath:error: method (or the URL equivalent when using URL instead of path string).

All of these strategies work perfectly in the iPad Simulator and on the iPad device, with the following exception: When I (successfully) build for Ad Hoc distribution, drag the .app and the .mobileprovision into iTunes, sync and then run the app on the device, the system no longer thinks the 'flds' folder exists! I've been trying to examine this for many hours, with no luck. Again, I'm perfectly sure it works both in the simulator and on the device running from Xcode, but not when synced via iTunes.

Ideas?

I discovered that the following call gets the folder path successfully also after syncing the iPad app via iTunes:

[[NSBundle mainBundle] pathForResource:@"flds" ofType:nil inDirectory:nil]

However, when I use this path to copy the folder to its new location in the Documents directory, only the folder itself and the .txt file in it (which I put in there as a test, it's not really supposed to be there) gets copied, and not the 6-7 empty subfolders. Again, the same pattern shows itself: Works as expected both on simulator and device running from Xcode ('flds' folder with 'test.txt' and empty subfolders are copied successfully), but not when synced via iTunes (only 'flds' folder with 'test.txt' are copied, not subfolders). Is this really the intended behaviour of copyItemAtPath:toPath:error:? And why would it behave differently after syncing via iTunes?

Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
hsdev
  • 507
  • 3
  • 12
  • I'm not sure if you've tried this, but have you checked to see if the folder flds exists in the final version's (the .app that you drag to iTunes) bundle by right clicking the app and selecting Show Package Contents? You should be able to locate the files and folders you're trying to access there via finder. – Mark Aug 11 '11 at 00:36
  • I've tried that. The folder is definitely there. I also tried putting a .txt file in one of the subfolders of 'flds' and accessing it via pathForResource:ofType:, and that actually works. Getting the folder itself doesn't. – hsdev Aug 11 '11 at 08:20
  • Please check if you are using a file which is not on the device itself. Maybe you tried to use a file which is saved on your computer. If you are not sure, try opening your project from another computer and see if it works. – Sagiftw Aug 11 '11 at 09:17
  • I'm perfectly sure I don't.. See edit for further development of this issue. – hsdev Aug 11 '11 at 09:20
  • Did you check mark the "Destination Box" which says **"Copy items destination group's folder (if needed) ** ? If not then delete the file from your project navigation and add it again with the check box selected. – Jimit Aug 17 '11 at 16:20

1 Answers1

0

In the end I figured out that copyItemAtPath:toPath:error: wouldn't copy empty folders, even though I think it's supposed to. And again, this behaviour only occured after syncing the app to the iPad via iTunes (when running from Xcode it did copy empty folders). My solution for the time being is to simply put a dummy text file in each subfolder, so that they're not empty anymore.

hsdev
  • 507
  • 3
  • 12