Add files to an Xcode project from a script?

XcodeScriptingProject

Xcode Problem Overview


Right now I'm using a few scripts to generate files that I'm including as resources in Xcode. The thing is I'm running the script, then deleting from the project, then adding back into the project. There must be a way to automate this last step, so that the script can generate the files and automatically add them into the xcode project for me.

I'm using bash but any language examples would help.

Thanks, Andrew

Xcode Solutions


Solution 1 - Xcode

I had a similar need as Andrew. I needed to be able to include resources from a script without knowing those resources ahead of time. Here's the solutions I came up with:

Add a new Run Script build phase after “Copy Bundle Resource” that contains the following command:

find -L ${SRCROOT}/SomeDerivedResources \
    -type f -not -name ".*" \
    -not -name "`basename ${INFOPLIST_FILE}`" \
    | xargs -t -I {} \
    cp {} ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/

Looks scary, but let’s break it down:

find -L ${SRCROOT}/SomeDerivedResources

This crawls the directory SomeDerivedResources in our source root (-L tells it to follow symbolic links)

-type f

Only include regular files

-not -name ".*"

Ignore files starting with a dot

-not -name "`basename ${INFOPLIST_FILE}`"

In my case, my Info plists live in my SomeDerivedResources directory so we need to exclude that file from being copied to our product

| xargs -t -I {}

Pipe the results of find into xargs with -t (echo resulting commands to stderr so they show up in our build log), -I (run the command once for each input file) and use {} as our argument placeholder

cp {} ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/

Lastly, copy each found file (denoted by {}) to our product’s resource directory.

I realized when typing this that using an rsync setup instead of cp could prevent us from copying resources each time you build. If your resources are very large it might be worth looking in to.

(Also, a folder reference wouldn’t work for my need for a few reasons. One, my icons are in my DerivedResources directory and having them in a subdirectory in the bundle seems not to work. Also, I ideally wanted to be able to use [UIImage imageNamed:@"MyAwesomeHappyImage.png"] and -pathForResource:ofType: (and some of my files are nested further inside my DerivedResources directory). If your needs don’t contain those restraints, I highly suggest you go the folder reference route.)

Solution 2 - Xcode

This can be done by adding a new build phase to your application.

  1. In your Xcode project browser, find the target for your application, and expand it to show all of the build phases.

  2. Add a new "run script" build phase to your target. The easiest way is to right-click on the target and choose "Add/New Build Phase/New Run Script Build Phase"

  3. Adding the new build phase should bring up an inspector window. In this window, you can enter the entire shell script, or simply a command line to run the script.

  4. Here's the gold: At the bottom of the inspector window you can specify input files and output files. Specifying input files sets up dependencies automatically (the shell script will only be executed if some of the input files have been modified). Specifying output files automatically propagates the dependencies to those files. When your shell script is run, Xcode knows that it needs to deal with those files that the shell script has modified.

  5. Be sure to drag your new build phase up to the top of the list of phases as shown in the screenshot below. The order will be important if you need those resource files to be included in the bundle.

  6. Save, build, commit to the repository, ask for a raise, get some fresh air and have a nice day! :)

Solution 3 - Xcode

For those with large number of files, to avoid having to recopy (or recheck) each file, as suggested by @Ben Cochran (thanks a lot for the great script), this is how to do it with rsync:

enter image description here

Solution 4 - Xcode

> Basically, the files just need to be copied into the main bundle

In that case just add a folder reference to the project (Create a folder in your project folder and then drag it into your projects "Resources" group (in the "Files & Groups" list; then in the sheet that appears select the "Create Folder References for any added Folder" radio button) and Xcode will copy the folder and all of its contents into the target bundle at build time.

Just an additional note: If you use this method to add image subfolders you'll have to prefix the image name with the subfolder name to use '[UIImage imageNamed:]'. For example if you have an image named "Rendezvous.png" in a subfolder named "MyImages":

`

// this won't work
UIImage * image = [UIImage imageNamed:@"Rendezvous"];
if (image) {
    NSLog(@"Found Rendezvous!");
} else {
    NSLog(@"Didn't find Rendezvous.");
}

// but this will!
image = [UIImage imageNamed:@"MyImages/Rendezvous"];
if (image) {
    NSLog(@"Found MyImages/Rendezvous!");
} else {
    NSLog(@"Didn't find MyImages/Rendezvous.");
}

`

Solution 5 - Xcode

If you already have the files somewhere on your system, relative to your source root, you can always add a "Copy Files" phase. This will allow you to specify a directory where your resources should be copied from.

You can combine this with the Build Script phase answer provided to you already. For instance, run a script to check out your assets from Subversion into a subdirectory of your project, and then follow that up with a Copy Files phase that copies from "$(SRCROOT)/Assets".

Solution 6 - Xcode

I know it's a bit late, but I just came across this article explaining how to do something that sounds like what you're looking for.

Solution 7 - Xcode

I found myself with a similar situation using Ionic Capacitor. What I was expecting was to include files on the "Copy Bundle Resources" bundle phase. What I found is that Ionic already packs you some inclusions and if you slip your files along this folders you get it included as well.

enter image description here

Do you see the App folder inclusion? It our entry point.

To include on it I add a script that do something like this:

cp -Rf ./includes/yourfolder/ ./ios/App/App/

Solution 8 - Xcode

I managed to solve the issue > "Code object is not signed at all"

that can be encountered during build upload to iTunes Connect in this way:

I didnot include the script to Bundle resources.

So the script (in this case Python file) is executed during build, (it does what it has to do) but it is not included in the bundle of the app.

How to do?

Open Build Phases, go to Copy Bundle Resources section, select the file and remove it with (-).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrewView Question on Stackoverflow
Solution 1 - XcodeBen CochranView Answer on Stackoverflow
Solution 2 - Xcodee.JamesView Answer on Stackoverflow
Solution 3 - XcodelucasartView Answer on Stackoverflow
Solution 4 - XcodegeowarView Answer on Stackoverflow
Solution 5 - XcodeMichael NachbaurView Answer on Stackoverflow
Solution 6 - XcodeRichardView Answer on Stackoverflow
Solution 7 - XcodeIgorView Answer on Stackoverflow
Solution 8 - XcodeSardorbek RuzmatovView Answer on Stackoverflow