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.

1

3 Answers 3

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...

Sign up to request clarification or add additional context in comments.

8 Comments

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
It should be the same case, you can add dummy image to the project and then replace it in build time.
Also there are number of tools on github and bitbucket that try to parse and manipulate the project file (here's one example bitbucket.org/darktable/mod-pbxproj/src/dde0e05c126a/… ) so you can try to clone one call it via your script.
Is there a way to do it without manipulating the xcodeproj file? For example, in this post: stackoverflow.com/questions/4884927/… 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.
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 d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/07/… . Then in your script you generate the image you want and you overwrite the dummy file. I think that should work fine
|
1

I ended up using build references.

1 Comment

Hey Pterie, can you please explain more about solution? Thanks is advance
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)
    }()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.