Adding actions before Xcode's build

Matheus V de Sousa
2 min readJul 24, 2020

--

Have you ever got yourself in a situation where every time you make a change to your development pod you need to clean (Cmd + K) your project's build folder?

Well, Xcode Schemes have this nice feature that allows us to add actions before and after building, running, testing, profiling, analyzing, and archiving our app.

We’re interested here in adding actions to the build phase, but take a look at all these options available. We could, for instance, add a post-action to our archiving phase to generate a fat library.

Press Cmd + <, to show the Edit Scheme window

By pressing Cmd + < , in the Xcode. Go to Build > Pre-actions. Press the + button to add a new run script action.

After that, select the scheme you want to run this configuration an insert the code below in the code area.

find ${BUILT_PRODUCTS_DIR} -name ‘your_framework_name*.framework’ -exec rm -rf {} \;

The $BUILT_PRODUCTS_DIR holds a reference to the product's built directory where we can find the built frameworks.

The whole path to a built framework in the product’s built directory inside the DerivedData

So we use the find command to find all files with .framework extension that have your_framework_name string in its name. Then we use the -exec rm -rf to remove those files.

Your configuration should like this.

This script will delete all frameworks starting with M2M in the product's built directory.

That's it! Adding a pre-action was just an excuse to show a bunch of useful stuff that you can tweak and explore. Go ahead and take a look at the other features available.

Let me know what you think about this article in the comments :)

--

--

Matheus V de Sousa
Matheus V de Sousa

Written by Matheus V de Sousa

iOS Developer. I'm posting some tips to help other people out there! :)