Resources not copied to bundle - ios

I have an XCode project for an iOS application, and added to it 3 small mp3's in the structure that are copied to the destination bundle upon compilation.
I have then added a bigger mp3's (4MB) to the structure (same folder than the previous ones) and into the Build Phases:
However upon compilation, this file is not present in the bundle. I tried to clean and recompile, change destination, but the file is still absent:
Anything I could have forgotten to check ?Thanks.

Go to your build phase->resources and check these files are showing there or not and check when you add them did you allow them to copy when required checkbox was selected or not.

When you add resources, make sure not only the copy items into destination group's folder (if needed) is ticked, but also tick the corresponding target in the Add to targets list.

Related

Whats the best way to add a dataset of 50 Folders to an iOS App?

In my case I have 50 Folders where each of it has subfolders and images. I could drag them into the apps main bundle but there are many duplicate filenames and it would be more practical to access them in the structured way.
Your wording "it would be more practical to access them in the structured way" seems to suggest that you think the Resources folder cannot contain a folder tree of files, it can. However if you let Xcode itself copy your resource files into the bundle it will flatten the tree without any option to preserve it (for reasons unknown). To address this you can copy the files into the bundle using a build script.
In outline, you need to fill in the gaps with some reading:
First add your files and folders into your project. Use one group per folder, in Xcode 9 creating a group creates a corresponding folder in the project directory but in prior versions you must create the group and then associate it with a folder – check the documentation of whatever Xcode version you are using.
Mark all the folders and files added in this way as not part of your build target. This prevents Xcode copying the files automatically into the bundle, and flattening your folder tree in the process.
Now in the target settings go to the "Build Phases" tab and add a new build script phase. Add a shell script which uses something like ditto to copy the folder tree into the bundle. Various environment variables are set which reference the project and the bundle, check your Xcode documentation or just run a dummy script and dump them out (it is an option, or use printenv). You can use these environment variables to determine the source and destination for your copy.
In your app itself you can locate the root folder of your tree using standard bundle methods. From there you can use whatever method you choose to traverse it/reference items with in it, in exactly the same way you would if the folder tree was not inside the bundle.
HTH

How can I add file to project in Xcode 9?

I created a new project in xcode 9.
I want to add image file to project.
When I delete this file from desktop,
Image file appears in red?
But in project folder, I find this image file.
In Xcode 8 its working fine,
Any idea?
It is an Xcode9-beta (9M136h) bug. I don't know if it has been resolved in beta3 or not. Copy items if needed copies the file to your project directory but still links to the original file.
What I do is, after dragging the file to my project, I select the folder icon (see red circle in the image below) from File Inspector and point to the copied file in the project directory.
#pesch's approach works too. First copy the image to your project folder and then drag it to Xcode.
Xcode 9 allows to have folders in Xcode synced with folder in your Finder structure. I assume this is a bug since Xcode 9 is still in beta and you are "ticking" the Copy items if needed.
Try moving the image to your project folder yourself and drag it from there to Xcode.
This is what you would do to leave the Copy items if needed box unticked since you are doing it yourself.
It looks like a XCode 9 bug. If you check the Full Path of your image file in Identity and Type Tab, it would show the location of the deleted file, hence the deleted(red) color.
You can delete that reference and can use Add File To.. option to add the reference of (already) copied image from your source folder.
Although this is not resource related (images, etc). The issue that I'm currently facing with the Xcode 9.0 GM build is that adding the items to the project does not actually added them to my targets, although I selected them.
Step 1 - Add Files to Project
Add New Files and Select your targets
Step 2 - View Files in File Inspector
You can clearly see in the file inspector that it is not added to those targets accordingly.
Step 3 - [Fix] Add New Added File to Targets
To Fix this, just check the targets that the files should be added to and you should be good :)
I have seen this issue in Xcode 9.0 GM (build 9A235, same as the current release). It's intermittent; sometimes 'Copy items if needed' is honored, sometimes not.
In one case it seemed to be triggered by the presence of source control. I was working through an online tutorial where the provided project had no source control. Adding files here did honor the 'copy items' checkbox, but if I made the provided project a git repository first, adding files ignored the checkbox every time.

Can I programmatically remove files from the Xcode "copy Bundle resources" list? from a command line tool?

The background:
An iOS app I'm building for a client is getting absurdly big due to a large number of multimedia files the client needs to add. (videos, high-res images, and sounds.)
The app drives it's UI from a set of page descriptor files that it reads at runtime. It uses those page descriptor files to load scenes from the app storyboard using unique IDs, and then each page's view controller figures out which multimedia content to display based on it's page descriptor.
I plan on adding a download_from_server flag to the page descriptor file that tells the app that it needs to download a zip file containing all the multimedia content for this page's view controller and all child pages from a server. At runtime, the app will see the flag, stop, and ask the user to tap OK to begin downloading the content, or click cancel to go back to the previous page. If they click OK the app will download the zip file, unzip it into the documents directory, and save a persistent "multimedia content downloaded" flag for that page. The app will know to look for multimedia assets in both the documents directory and the app bundle.
I will write a batch tool that walks the tree of file descriptors. When it finds a descriptor who's download_from_server flag is set, it will build a zipfile from the multimedia content for that page and all of it's child pages. I'll then upload all those zipfiles to the server.
My Question:
Can I make my batch tool modify Xcode's settings for the multimedia files in the project that are being uploaded to the server so those files are no longer copied into the app bundle, and if so, how? I want to be able to delete those entries from the "Copy Bundle Resources" build phase file list. (Or un-check the target membership checkbox, which I believe does the same thing.)
I am not exepert in the intricacies of makefiles and build rules, so I need guidance.
EDIT:
It appears that Xcode uses entries in the project's project.pbxproj file to track which files are copied into the application at build time.
When I made a copy of the project.pbxproj from a project, un-checked the "target membership" checkbox for a file, and made another copy of the project.pbxproj file, and compared them, I found 2 differences. When the file in question has it's "target membership" checkbox checked, it is listed in 3 places in project.pbxproj, and when the checkbox is not checked, it is only listed in 2 places.
The 2 entries that are removed are:
An entry in what looks like an "objects" dictionary at the top of the file, marked with a comment line
/* Begin PBXBuildFile section */
And in a section marked with a comment /* Resources *, in a list with the key "files", that appears to be an array of file IDs (long hex strings.)
Whether the file's target membership checkbox is checked or not, it appears in another array of file IDs marked with the comment /* Begin PBXFileReference section */.
It looks to me like I could delete the entry line in the objects dictionary and in the array of file IDs in the Resources section.
Can anyone reading this chime in on the safety and effectiveness of programmatically modifying an Xcode project's project.pbxproj file to remove files from the Copy Bundle Resources build phase?
Instead of manually editing the pbxproj file, you can use a ruby gem called xcodeproj to edit project settings. (Cocoapods uses this also so create pod targets and add files to them). This would be a good starting point.
I don't know how familiar you are with Ruby, but it would worth to check out the Project class and its native_targets array property. You can find your desired target by name and you can access the resource build phase via the resource_build_phase property. You can write a Ruby script like this (not fully checked):
project = Xcodeproj::Project.open('path to .xcodeproj')
target = project.native_targets
.select { |target| target.name == 'YourTargetName' }
.first
target.resource_build_phase.files.each do |f|
f.remove_from_project
end
project.save

Resources in custom iOS frameworks

I have made an empty single-view application in Xcode 6 (FWTest) and added a Cocoa Touch Framework (FWTestKit) as a target and asked it to embed in FWTest. Then I add an image (photo.png) to the framework, that I expect to be in Frameworks/FWTestKit.framework/Versions/A/Resources/photo.png when I build & archive my app. However, I find it in Frameworks/FWTestKit.framework/photo.png
How can I make it at least be in a Resources folder? Preferably a versioned folder so I can ship different versions of my framework?
When I add this framework to another app, do I need to do anything special to make sure the resources are bundled along with it, so that when I reference a resource from within my framework I can be sure it'll be available also when used in another app?
Cheers
Nik
You should add your photo.png into separate folder (let's call it "gica" ). Then drag folder into project, check Copy items if needed and create folder reference. Then (important) in build phases, drag photo.png again in Copy Bundle Resources, keeping Copy items if needed and create folder references. After you compile you should have now desired folder structure [It is keeping gica/photo.png].
I think you need to revise the architecture about versioning. Anyway if you still need to place the image inside the folder. Try creating a run script.
In your build phases instead of adding your images to copy bundle resource phase. You can create a shell script to copy your images to desired folder. Please note build phases are run in the order they placed. My point is you can customise xCode Build process through your own shell scripts.

Xcode copies images everytime I compile my project.

Whenever I build my current iPhone project, it copies every single picture I've added to the project into a new resources folder.
I didn't realize this until today but now I have a resource folder thats 1GB full of the same photos that have been copied over and over again.
When I build it says: Copying xAmount of xAmount resources to product
Is there anyway for me to turn this off so it only copies new images that have been added?
Thanks.
when you import resource files on your project, be sure to have: "copy items into destination group's folder if needed" option checked.

Resources