A Tamperproof, Swift-Only Approach:
- Add a new Run Script build phase to your app and MAKE SURE it is set to run before the Compile Sources phase.
- Add this as the code in that script:
#!/bin/bash
timestamp=$(date +%s)
echo "import Foundation;let appBuildDate: Date = Date(timeIntervalSince1970: $timestamp)" > ${PROJECT_DIR}/Path/To/Some/BuildTimestamp.swift- Create the file BuildTimestamp.swift at some path in your project, then make sure the output path in the script above matches where that file exists, relative to the project's root folder.
- You now have a global appBuildDate that can be used anywhere in your project. (Build the project once before using the variable so that the script creates it in the file you specified.)
- Optional: if you'd like the date to update in incremental builds, be sure to uncheck the "based on dependency analysis" checkbox in the Run Script phase you created. Advantages:
- It's automatic.
- It can't be affected by users changing the modification/creation date of various files in the app bundle (a concern on macOS).
- It doesn't need the old TIME and DATE from C.
- It's already a Date and ready to be used, as-is.