4

I have a file that is created by a script that is run during a build phase. Consequently, the file does not exist until build time.

How can I add the file to the app/project so that my code can access it at run time?

I know what the filename will be.

Normally, if I created the file by hand, I would just drag/drop it into Xcode, and I can instantly access it by using the filename in code. Since I am generating the file during build, I want to know how I can add it to my project.

EDIT: The file that I am adding is an Image file, that is created by a script during a build phase. I want to be able to access this file at runtime in my product.

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
  • Found the answer in this post: http://stackoverflow.com/questions/4884927/build-phase-that-create-a-plist-and-copy-it-in-resource-bundle – Rahul Iyer Mar 25 '12 at 13:14

3 Answers3

2

I ended up using build references.

Ryan Wersal
  • 3,210
  • 1
  • 20
  • 29
Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
1

You can copy the generated file to the build output using a script like this one I use to copy a plist with values maintained outside version control, as anything copied to "${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}/" is available using NSBundle APIs.

SECRETS="${SRCROOT}/../../secrets/Secrets.plist"
APP="${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}/Secrets.plist"
if [ -f "$SECRETS" ]; then
    cp -f "$SECRETS" "$APP"
    echo "copied Secrets.plist: $APP"
else
    echo "Secrets.plist not found at $SECRETS, all secrets will be 'MISSING'"
fi

For instance, that plist of secrets is defined as the Decodable struct SecretsFile and loaded with

    private static var file: SecretsFile? = {
        guard let url = Bundle.main.url(forResource: "Secrets", withExtension: "plist"),
              let data = try? Data(contentsOf: url) else { return nil }
        return try? PropertyListDecoder().decode(SecretsFile.self, from: data)
    }()
Alex Curylo
  • 4,744
  • 1
  • 27
  • 37
1

You could create the file and add it to the project and then during the build phase, your script could replace the existing file with the generated one....

The other option is to try manipulating the project file itself but that's quite complicated. Here are some resources on the subject...

Community
  • 1
  • 1
iffi
  • 258
  • 3
  • 7
  • I don't need to compile the file - it is an image. The image is created by a script during a build phase. I want to add it to the product so that my app can access it from code at runtime – Rahul Iyer Mar 22 '12 at 10:22
  • It should be the same case, you can add dummy image to the project and then replace it in build time. – iffi Mar 22 '12 at 10:46
  • Also there are number of tools on github and bitbucket that try to parse and manipulate the project file (here's one example https://bitbucket.org/darktable/mod-pbxproj/src/dde0e05c126a/mod_pbxproj.py ) so you can try to clone one call it via your script. – iffi Mar 22 '12 at 10:47
  • Is there a way to do it without manipulating the xcodeproj file? For example, in this post: http://stackoverflow.com/questions/4884927/build-phase-that-create-a-plist-and-copy-it-in-resource-bundle The file is just copied into the app. I'm not sure if this is the right way. So I was wondering if I should do it. Also, for some reason my product is always "red" in Xcode. – Rahul Iyer Mar 22 '12 at 10:59
  • My simpler idea is the following. You create a dummy image, you add it to the project and you add it to the Copy Bundle Resources phase of your target https://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/07/FragShaderCopyBundleResources.jpg . Then in your script you generate the image you want and you overwrite the dummy file. I think that should work fine – iffi Mar 22 '12 at 13:28
  • About the 'red' products, if you build the product for device it will stop being red and will appear fine http://stackoverflow.com/questions/5409015/app-file-appears-in-red-missing-in-new-projects-with-xcode-4 – iffi Mar 22 '12 at 13:29
  • Thanks iffy. But the problem is there could be many files, so created the dummy files may be too time-consuming. What did you think about the link I posted? – Rahul Iyer Mar 22 '12 at 13:32
  • I think it might work, you can also have a look at this answer and the comments after it http://stackoverflow.com/a/2267766/1285576 – iffi Mar 22 '12 at 14:30