I have decided to summarize whatever new I learnt in the week.
This week I learnt about how to define macros for Swift and Objective-C / C/C++. I was trying to modify the jenkins pipeline script to add parameters to existing set of macros. based on the type of git branch.
Hypothetical example: The requirement is to create builds either for developers, Internal testing, beta or production. This will be categorized based on branch type.
- If the build is built for feature or bugfix branch then it will be a developer build.
- If its for a PR or master(main) branch then it would be for internal testing.
- If its for a beta / staging branch then it would be for beta testing.
- If its for a release branch then it would be for production.
So now we have 4 types of builds. Based on the type of build I want to set the configuration at runtime. For example: Fetch data from endpoints but domain would different for each build type.
There is no one way to achieve this. Let’s take the macros way first. Set the macros while creating the build using xcodebuild command.
To set the macros while using xcodebuild command we use GCC_PREPROCESSOR_DEFINITIONS
GCC_PREPROCESSOR_DEFINITIONS=”$GCC_PREPROCESSOR_DEFINITIONS =1″ // this works for Objective-C and C/C++
But this cannot be read by Swift code. So what about Swift? We use SWIFT_ACTIVE_COMPILATION_CONDITIONS
SWIFT_ACTIVE_COMPILATION_CONDITIONS=”SWIFT_ACTIVE_COMPILATION_CONDITIONS ”
For Swift we should not append **
=1**
to macro_name.
I spent half a day to figure out how to pass macros to Swift via command line and then figure out we aren’t supposed to give =1
. Phew.
There is another way to solve our first requirement. Using Info.plist , by adding parameters to Info.plist of the app. We use **plutil**
to add, replace, remove and extract keys and values from a plist.
- plutil – replace -string // replaces if there are any exists or inserts
- plutil – insert -string // inserts, but fails if already exists
- plutil – remove // removes any key value pair if the key exists
- plutil – extract xml1 -o – | sed -n “s/.(.)<\/string>.*/\1/p” // the piped regex is to get the string value
Instead of -string
you can give any data type which property lists accept.
Important thing to note: Do not modify Info.plist after the build is successful. The code-sign throws error or your package is marked as malicious and could not be installed.
So modify the Info.plist just before the xcodebuild command is executed.
Top comments (0)