Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing custom variables for Info.plist

Tags:

xcode

plist

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:

  • EXECUTABLE_NAME
  • PRODUCT_BUNDLE_IDENTIFIER
  • PRODUCT_NAME

enter image description here

My question: how do you create your own variable for inclusion in the plist?

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.

like image 225
jeff-h Avatar asked Oct 14 '25 08:10

jeff-h


1 Answers

There are two possible solutions to add key/values dynamically to your Info.plist:

Preprocess Info.plist file

  1. In Build Settings set Preprocess Info.plist file to Yes.
  2. Then set YourProject/Info.h to Info.plist Preprocessor Prefix File
  3. Create Info.h file with next content and add it to your project:
#define BUILD_DATE __TIMESTAMP__
  1. Create key/value in your Info.plist:
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

  1. Create 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)
}
like image 56
iUrii Avatar answered Oct 17 '25 09:10

iUrii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!