Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resource files in subdirectory are copied to root directory of the app bundle

Tags:

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.

like image 712
neevek Avatar asked Jun 10 '12 14:06

neevek


People also ask

What is root directory and subdirectory?

Operating Systems Tree-structured directories (sometimes called hierarchical directories) allow users to create directories within directories at will. A master or root directory contains pointers to main subdirectories. Data files can be mentioned in these subdirectories, or further, sub-subdirectories can be created.

What is root subdirectory?

Subdirectories may refer to folders located directly within a folder, as well as folders that are stored in other folders within a folder. For example, the main directory of a file system is the root directory. Therefore, all other folders are subdirectories of the root folder.

How do I add resources to Xcode?

Just drag the resource file (from a finder window) into your project files area (left side) and drop in supporting files. Once you drop, a dialog will prompt you which targets to add it for.


1 Answers

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

like image 135
Alexei Sholik Avatar answered Oct 13 '22 19:10

Alexei Sholik