I'm using __DATE__
and __TIME__
in Objective-C to get the build date and time of my app. I can't find a way to get this information in Swift. Is it possible?
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())
__LINE__
or __FILE__
in Swift. This same approach will just return the line/file of the Objective-C code, obviously. –
Visitor #file
, #line
, #column
, #function
in Swift 3. –
Misanthrope #date
in Swift, nor anywhere in Apple's documentation. Can you add a link / full example of its use? –
Danley #date
is a thing. I've updated the answer. –
Visitor You can get the build date and time without reverting to objective-C. When the app is built, the Info.plist file placed in the bundle is always created from the one in your project. So the creation date of that file matches the build date and time. You can always read files in your app's bundle and get their attributes. So you can get the build date in Swift by accessing its Info.plist file attributes:
var buildDate:NSDate
{
if let infoPath = NSBundle.mainBundle().pathForResource("Info.plist", ofType: nil),
let infoAttr = try? NSFileManager.defaultManager().attributesOfItemAtPath(infoPath),
let infoDate = infoAttr["NSFileCreationDate"] as? NSDate
{ return infoDate }
return NSDate()
}
Note: this is the post that got me to use the bridging header when I initially had this problem. I found this "Swiftier" solution since then so I thought I'd share it for future reference.
[EDIT] added compileDate variable to get the latest compilation date even when not doing a full build. This only has meaning during development since you're going to have to do a full build to release the application on the app store but it may still be of some use. It works the same way but uses the bundled file that contains the actual code instead of the Info.plist file.
var compileDate:Date
{
let bundleName = Bundle.main.infoDictionary!["CFBundleName"] as? String ?? "Info.plist"
if let infoPath = Bundle.main.path(forResource: bundleName, ofType: nil),
let infoAttr = try? FileManager.default.attributesOfItem(atPath: infoPath),
let infoDate = infoAttr[FileAttributeKey.creationDate] as? Date
{ return infoDate }
return Date()
}
__DATE__
since when you update your app info.plist will remain the same while __DATE__
gets proper updated by the time of the update. –
Oscan 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())
__LINE__
or __FILE__
in Swift. This same approach will just return the line/file of the Objective-C code, obviously. –
Visitor #file
, #line
, #column
, #function
in Swift 3. –
Misanthrope #date
in Swift, nor anywhere in Apple's documentation. Can you add a link / full example of its use? –
Danley #date
is a thing. I've updated the answer. –
Visitor Swift 5 version of Alain T's answer:
var buildDate: Date {
if let infoPath = Bundle.main.path(forResource: "Info", ofType: "plist"),
let infoAttr = try? FileManager.default.attributesOfItem(atPath: infoPath),
let infoDate = infoAttr[.modificationDate] as? Date {
return infoDate
}
return Date()
}
A Tamperproof, Swift-Only Approach:
Add a new
Run Script
build phase to your app and MAKE SURE it is set to run before theCompile 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.In Build Settings, search for "Enable User Script Sandboxing" and turn that OFF. (Otherwise you'll get a permissions error when the script tries to run.)
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.
A slight variation on previous answers, checking the executable creation date instead. This seems to work on macOS too (tested with a Catalyst app).
/// Returns the build date of the app.
public static var buildDate: Date
{
if let executablePath = Bundle.main.executablePath,
let attributes = try? FileManager.default.attributesOfItem(atPath: executablePath),
let date = attributes[.creationDate] as? Date
{
return date
}
return Date()
}
All the older answers here are not good, as they do not provide a steady and reliable way to get the actual build date. For instance, getting the file date of a file inside the app is not good because the file date could change without invalidating the app's code signature.
The official build date is added by Xcode to the app's Info.plist – that's the one you should be using.
E.g, with this code (sorry, it's in ObjC, but transscribing it to Swift shouldn't be so hard):
+ (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;
}
And this is the script you'll have to run from your project's Build Phases in order to add the BuildDateString
to your Info.plist
:
#!/bin/sh
infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
builddate=`date +%Y-%m-%dT%H:%M:%S%z`
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
BuildDateString
added to the plist? Xcode isn't automatically doing that for me, at least not for macOS apps. –
Nils Relying on the creation date of the Info.plist won't work. The retrieved result in some scenarios can be the date-timestamp of when the app gets installed into your computer, which is what actually happened to me.
Here are my two thoughts:
Use
contentModificationDateKey
instead. Still, this might be unreliable if one copied this one to FAT or NTFS volume, ruining the timestamp information.Find a method to get the CFDate value of
kseccodeinfotimestamp
. This is not tamperable. See the following example:
(It'll return nil if not signed by Apple Developer ID Application, etc. Ad-hoc signatures will let it throw nil, too.)
// (c) 2021 and onwards The vChewing Project (MIT-NTL License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that:
// No trademark license is granted to use the trade names, trademarks, service
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.
import Foundation
let url = URL.init(fileURLWithPath: "/Users/shikisuen/Library/Input Methods/vChewing.app/")
func getCodeSignedDate(bundleURL: URL) -> Date? {
var code: SecStaticCode?
var information: CFDictionary?
let status4Code = SecStaticCodeCreateWithPath(bundleURL as CFURL, SecCSFlags(rawValue: 0), &code)
guard status4Code == 0, let code = code else {
NSLog("Error from getCodeSignedDate(): Failed from retrieving status4Code.")
return nil
}
let status = SecCodeCopySigningInformation(code, SecCSFlags(rawValue: kSecCSSigningInformation), &information)
guard status == noErr else {
NSLog("Error from getCodeSignedDate(): Failed from retrieving code signing intelligence.")
return nil
}
guard let dictionary = information as? [String: NSObject] else { return nil }
guard dictionary[kSecCodeInfoIdentifier as String] != nil else {
NSLog("Error from getCodeSignedDate(): Target not signed.")
return nil
}
guard let infoDate = dictionary[kSecCodeInfoTimestamp as String] as? Date else {
NSLog("Error from getCodeSignedDate(): Target signing timestamp is missing.")
return nil
}
return infoDate as Date
}
if let infoDate = getCodeSignedDate(bundleURL: url) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd.HHmm"
dateFormatter.timeZone = .init(secondsFromGMT: +28800) ?? .current
let strDate = dateFormatter.string(from: infoDate)
print(strDate)
}
© 2022 - 2024 — McMap. All rights reserved.