Build Information in iOS Application (Date/Time App Was Built)

Build information in iOS Application (date/time app was built)

You can write a shell script build phase in Xcode that runs at the end of your build process. In this phase you can use the defaults command to write data to an arbitrary file. I've used this technique to write to the Info.plist file, but you can write to any file you want[1].

Here's a sample script to write the current git version to Info.plist:

infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
gitversion="$(cd "$SRCROOT" && git describe --always --dirty 2>/dev/null)"
if [[ -n "$gitversion" ]]; then
defaults write "${infoplist%.plist}" GitVersion "$gitversion"
fi

You should be able to adapt this to point to the file you want (e.g. your Settings bundle) and write the info you want.

[1] Be careful if you write to Info.plist, Xcode has bugs that can prevent it from realizing Info.plist changed during the build, which can break the provisioning when doing a device build.

Showing App's Build Date

Try running the script as a build phase step, rather than a scheme pre-action step, so it's run all the time, regardless of the type of build you are producing.

Get build date and time in Swift

You can use #line, #column, and #function.


Original answer:

Create a new Objective-C file in your project, and when Xcode asks, say yes to creating the bridging header.

In this new Objective-C file, add the following the the .h file:

NSString *compileDate();
NSString *compileTime();

And in the .m implement these functions:

NSString *compileDate() {
return [NSString stringWithUTF8String:__DATE__];
}

NSString *compileTime() {
return [NSString stringWithUTF8String:__TIME__];
}

Now go to the bridging header and import the .h we created.

Now back to any of your Swift files:

println(compileDate() + ", " + compileTime())

how to get XCode to add build date & time to Info.plist file

The code in Michael's answer is incorrect or no longer up to date. The version below fixes an error in the set syntax and also supports build paths with spaces in them.

infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
builddate=`date`
if [[ -n "$builddate" ]]; then
# if BuildDateString doesn't exist, add it
/usr/libexec/PlistBuddy -c "Add :BuildDateString string $builddate" "${infoplist}"
# and if BuildDateString already existed, update it
/usr/libexec/PlistBuddy -c "Set :BuildDateString $builddate" "${infoplist}"
fi

Note: This change was submitted as an edit but got rejected and I don't yet have enough reputation to post a comment on his answer...

How to determine the date an app is installed or used for the first time?

My solution would be to check the last modified date of one of the files in the app bundle.

NSString *sourceFile = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Icon.png"];

NSDate *lastModif = [[[NSFileManager defaultManager] attributesOfItemAtPath:sourceFile error:&err] objectForKey:NSFileModificationDate];

How to get iOS APP archive date using Swift

You can get the url of your app using Bundle property executableURL and use url method resourceValues to get the bundle creation date:

if let executableURL = Bundle.main.executableURL,
let creation = (try? executableURL.resourceValues(forKeys: [.creationDateKey]))?.creationDate {
print(creation)
}


Related Topics



Leave a reply



Submit