24

I am not sure whether this is expected or it is bug. I am trying to create the same folder structure in disk as that in XCode. I use the following steps to add resource files to my project:

  1. Create a Resources folder under the root folder of the project.
  2. Drag the Resources folder to XCode, which creates a Group for the folder.
  3. Drag my resource files from Finder to the Resources folder in XCode.

And now I can see all the resource files are listed in Build Phrases->Copy Bundle Resources.

The weird thing is that [[NSBundle mainBundle] pathForResource:@"book" ofType:@"epub"] returns a file path that can be located, while [[NSBundle mainBundle] pathForResource:@"book" ofType:@"epub" inDirectory:@"Resources"] returns (null).

From the Build Results window, I notice this line:

CpResource MBookReader/Resources/book.epub /Users/neevek/Library/Developer/Xcode/DerivedData/workspace-fykuaikryknuhxgevcdtspkspptj/Build/Products/Debug-iphonesimulator/MBookReader.app/book.epub
cd /Users/neevek/workspace/xcode_projects/MBookReader
setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
builtin-copy -exclude .DS_Store -exclude CVS -exclude .svn -exclude .git -exclude .hg -resolve-src-symlinks -strip-tool /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip /Users/neevek/workspace/xcode_projects/MBookReader/MBookReader/Resources/book.epub /Users/neevek/Library/Developer/Xcode/DerivedData/workspace-fykuaikryknuhxgevcdtspkspptj/Build/Products/Debug-iphonesimulator/MBookReader.app

which shows that Resources/book.epub file is indeed copied to MBookReader.app/(the root directory of the app bundle).

Now Resources is a real folder in disk, why the folder itself is not copied?

PS. I am using XCode 4.3.2.

neevek
  • 11,760
  • 8
  • 55
  • 73
  • I ran into this same problem when putting the "Help Book" files into my app; they need to be in a subdirectory under Resources. android's answer worked for me too! – Marc May 15 '13 at 00:30

1 Answers1

41

When adding your Resources folder to Xcode, choose "Create folder references" instead of "Create groups for any added folders" in the sliding window.

That is, replace the 3 steps in your question with the following ones.

  1. Create a Resources directory under the root directory of the project. Organize your directory structure inside the Resources directory the way you want it to be.
  2. Drag the Resources directory to XCode and select the Create folder references option.

You're done. Xcode will copy the contents of your Resources directory recursively into the bundle.

You might have trouble further down the road when Xcode doesn't copy a modified file somewhere inside the Resources directory. A clean build usually fixes this. Alternatively, you can remove the .app file before building. This will not cause unmodified source files to rebuild, but will force Xcode to copy all of the resources anew.

Sample Folder Reference

Alexei Sholik
  • 7,287
  • 2
  • 31
  • 41
  • 3
    No matter whether it is a real directory or just a reference, it seems that all the resource files will be copied to the root directory of the app bundle regardless what directory they reside when the project is built. – neevek Sep 12 '12 at 14:51
  • Hi @android, that didn't work, I follow the steps you listed, and when I try to run the app, it hangs at the point `attaching to TestApp`, the simulator keeps showing a black screen, the app won't startup. – neevek Sep 14 '12 at 17:22
  • @Neevek This problem is unrelated to how you add resources to your app bundle. Try launching the app without a debugger. You can also look at the .app file produced by Xcode without launching the app to check that the directory structure is correct within the bundle. – Alexei Sholik Sep 15 '12 at 06:59
  • No way, even deleting and repeating this, comes to getting your files on root of bundle, it seems Xcode 5 is crappy software... – Frederic Yesid Peña Sánchez Jun 17 '14 at 07:22
  • 3
    Thanks, worked for me (XCode 7.3.x). However a word of warning - don't name the directory "Resources" or you will go into strange problems like [Code sign error](http://stackoverflow.com/a/29271922/1656515) – Mikolaj Jun 10 '16 at 11:59
  • I'm trying to follow this but it only worked on simulator, not real device. Any ideas? – tomtclai Aug 03 '16 at 23:00