jeff-h
jeff-h

Reputation: 2639

Providing custom variables for Info.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:

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.

Upvotes: 4

Views: 6355

Answers (1)

iUrii
iUrii

Reputation: 13788

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)
}

Upvotes: 3

Related Questions