Reputation: 2639
In Xcode, you can edit an app's Info.plist, and you will see a number of variables that are inserted into the plist dynamically.
In the following screenshot you can see for example:
I would like to add the Build Date to the plist, dynamically generated every time Xcode creates a new build. However I cannot see where to set up the variable for dynamic inclusion as per the system-provided examples above.
I've tried export BUILD_DATE=$(date +'%Y-%m-%d')
in a build phase but putting $(BUILD_DATE)
as a plist value just isn't giving me anything.
Upvotes: 4
Views: 6355
Reputation: 13788
There are two possible solutions to add key/values dynamically to your Info.plist:
Preprocess Info.plist file
Build Settings
set Preprocess Info.plist file
to Yes
.YourProject/Info.h
to Info.plist Preprocessor Prefix File
#define BUILD_DATE __TIMESTAMP__
BuildDateTime String BUILD_DATE
Now your Info.plist will have next record after build:
<key>BuildDateTime</key>
<string>"Wed Nov 10 01:52:49 2021"</string>
To access the build date string value you can use next code:
if let date = Bundle.main.object(forInfoDictionaryKey: "BuildDateTime") as? String {
print(date)
}
PlistBuddy
Run Script
in Build Phases
with next command:/usr/libexec/PlistBuddy -c "Add :BuildDateTime date `date`" "${BUILT_PRODUCTS_DIR}/${EXECUTABLE_FOLDER_PATH}/Info.plist"
Now your Info.plist will have next record after build:
<key>BuildDateTime</key>
<date>2021-11-09T23:59:56Z</date>
To access the build date value you can use next code:
if let date = Bundle.main.object(forInfoDictionaryKey: "BuildDateTime") as? Date {
print(date)
}
Upvotes: 3