Power of Xcconfigs in Xcode
So this isn’t just a tutorial on how to use configuration files in Xcode; it also talks about the emotion behind improving and using xcconfig files. This idea hit me when I had to make clones of my project. So why should you create a new project when we have GOD’s gift in our hands’ configuration files, amigos?
For those who don’t know about configuration files, read about it in my previous article:
Using .xcconfig files to store various SDK keys and IDs inside Xcode Project.
Now, let’s start by creating a project called GodsGift. Install one or two pods to your project because most require third-party help. If you don’t need it, it’s icing on the cake, and you can declare yourself the Supreme Power.
If you’ve installed your pods in any of your projects, open their workspace file. If your project doesn’t require any external pods, open your xcodeproj file, and you’ll see two default configurations, i.e., Debug and Release. These are shown below:
In the image above, you can see that Pods-GodsGift.debug is connected to the Debug configuration, and Pods-GodsGift.release is connected to the Release configuration. Now we can create two configurations in each project, Development and Production, rename the default Debug Release to the Project name, and add new files via the + button in Configurations.
Project1 and Project2 are shown in the above image; they’re made with the development and production configurations. But all of the development and production is linked to the same type of .xcconfig files: Pods-xxxxxxxx.xxxx. So now, we’ll manually create four .xcconfig files related to our projects.
Here are the files we just created:
Now, use these files in your Xcode project’s configurations as shown below:
Now, build your project.
Uh-oh. Your project got an error related to Pods-xxxxxxx.xcfilelist.
So, knowing this, we need to import our Pods’ debug and release xcfilelist into our manually generated .xcconfig files. This is recommended whenever you create new xcconfig files.
First, uninstall the pods, then reinstall them. A new xcfilelist will be created; it’s related to the xcconfig file used in the configuration we imported inside the four xcconfig files we created earlier.
Now, we’re done importing the red-marked files into our green-marked files. The yellow areas in the above image can help you see this.
Use this code to add these files to the xcconfig files you created manually:
#include "Pods/Target Support Files/Pods-PROJECT_NAME/Pods-PODS_XCONFIG_NAME.xcconfig"
Now that your error related to Pods-xxxxxxx.xcfilelist is gone, you can run and try it.
Inside of your xcconfig files, you can put anything like BundleId, Version, Build Version, BasePath, FacebookID, Google Plist file name, any SDK keys, APP_ICON asset name, and Launch Screen Name that is different for each app.
And you can use these things dynamically in the Project Build Settings by setting the $(XCCONFIG_KEY), so now if you change anything in the xcconfig file, it will be changed in the app also. After using the xcconfig files, don’t you dare change these things manually from Xcode. Change these things from xcconfig files only. Otherwise, your work will go to crap because of a single mistake, Ha, ha!
So, do it carefully!
Let’s create some temporary keys inside the xcconfig files.
As you see in the above file, we created all the required keys to use Build settings with $(KEY) to use these things, and you are just done with these things.
Now, the last step is to create schemes via manage schemes for each Configuration to use.
Create four schemes dev prod for each project, i.e., Project1 and Project2. This will result in four schemes using containers in the GodsGift project.
OK, the schemes are done now. Just double-click on each scheme to select its configuration for Debugging, Archiving, Test, Run, Analyze, etc. Please make sure you assign the correct configs and watch out.
Now you can switch your projects easily while debugging, running, archiving, etc.
If you want to access any configuration value inside your Swift code, one more step is to add these keys to your plist file with the following code:
<key>KEY</key>
<string>$(KEY)</string>
Use this code to access these key values via plist:
e.g., let appName = Configuration.getValue(for: .APP_APP_NAME)
Thanks for reading this. Try and implement it in your project. If any issues occur, feel free to reach out to me in the comments.
Happy coding. Love what you do and feel it 😍😍😍.
Power of .xcconfigs in Xcode was originally published in Better Programming on Medium, where people are continuing the conversation by highlighting and responding to this story.