Showing App's Build Date

Viewed 8137

I am able to display the build date for my app in the simulator, but whenever I archive the app, and upload it to TestFlight, and then install it on a device, the build date doesn't show.

Here is what I'm doing to display the build date.

First, I added CFBuildDate as a string to myproject-info.plist

Next, I added the following script to Edit Scheme -> Build -> Pre-Actions -> Run Script Action :

infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
builddate=`date`
if [[ -n "$builddate" ]]; then
/usr/libexec/PlistBuddy -c "Add :CFBuildDate $builddate" ${infoplist}
/usr/libexec/PlistBuddy -c "Set :CFBuildDate $builddate" ${infoplist}
fi

Finally, used the following code to get the build date from the plist file :

NSString *build_date = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBuildDate"];

This displays the build date in simulator (though occasionally it doesn't), but when deploying the app through TestFlight, the build date never displays. Any ideas ?

Thanks in advance.

7 Answers

On macOS, I use this code, which fetches the build date from the app's Info.plist content. Might also work on iOS, I haven't checked:

+ (NSDate *)buildDate {
    static NSDate *result = nil;
    if (result == nil) { 
        NSDictionary *infoDictionary = NSBundle.mainBundle.infoDictionary;
        NSString *s = [infoDictionary valueForKey:@"BuildDateString"];
        NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
        NSDate *d = [formatter dateFromString:s];
        result = d;
    }
    return result;
}

Swift5

let compileDate = String(Date())
let df = DateFormatter()
df.dateFormat = "MMM-dd-yyyy"
let usLocale = NSLocale(localeIdentifier: "en_US")
df.locale = usLocale
let aDate: Date? = df.date(from: compileDate)
Related