xcode 显示应用程序的构建日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11779403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing App's Build Date
提问by Myxtic
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.
我能够在模拟器中显示我的应用程序的构建日期,但是每当我存档应用程序并将其上传到 TestFlight,然后将其安装在设备上时,构建日期都不会显示。
Here is what I'm doing to display the build date.
这是我为显示构建日期所做的工作。
First, I added CFBuildDateas a string to myproject-info.plist
首先,我将CFBuildDate作为字符串添加到 myproject-info.plist
Next, I added the following script to Edit Scheme -> Build -> Pre-Actions -> Run Script Action :
接下来,我将以下脚本添加到 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 :
最后,使用以下代码从 plist 文件中获取构建日期:
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 ?
这会在模拟器中显示构建日期(尽管偶尔不会),但是当通过 TestFlight 部署应用程序时,构建日期永远不会显示。有任何想法吗 ?
Thanks in advance.
提前致谢。
采纳答案by trojanfoe
Try running the script as a build phasestep, rather than a scheme pre-action step, so it's run all the time, regardless of the type of build you are producing.
尝试将脚本作为构建阶段的步骤运行,而不是计划前操作步骤,因此无论您生成的构建类型如何,它都会一直运行。
回答by Jeremy
You might consider using the built-in __DATE__
and __TIME__
macros which will return a string representation of the date and time the app was built. Perhaps they will be of more help to you:
您可能会考虑使用内置__DATE__
和__TIME__
宏,它们将返回构建应用程序的日期和时间的字符串表示。或许它们对你更有帮助:
NSString *dateStr = [NSString stringWithUTF8String:__DATE__];
NSString *timeStr = [NSString stringWithUTF8String:__TIME__];
回答by Dirk Schmidt
To get build date with format 'yyMMddHHmm' you could try this:
要获取格式为“yyMMddHHmm”的构建日期,您可以尝试以下操作:
+ (NSString *)GetBuildDate {
NSString *buildDate;
// Get build date and time, format to 'yyMMddHHmm'
NSString *dateStr = [NSString stringWithFormat:@"%@ %@", [NSString stringWithUTF8String:__DATE__], [NSString stringWithUTF8String:__TIME__]];
// Convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"LLL d yyyy HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Set output format and convert to string
[dateFormat setDateFormat:@"yyMMddHHmm"];
buildDate = [dateFormat stringFromDate:date];
[dateFormat release];
return buildDate;
}
回答by neoneye
Swift3
斯威夫特3
static var buildDate: Date? {
guard let infoPath = Bundle.main.path(forResource: "Info.plist", ofType: nil) else {
return nil
}
guard let infoAttr = try? FileManager.default.attributesOfItem(atPath: infoPath) else {
return nil
}
let key = FileAttributeKey(rawValue: "NSFileCreationDate")
guard let infoDate = infoAttr[key] as? Date else {
return nil
}
return infoDate
}
static var prettyBuildDate: String {
guard let date = buildDate else {
return "Unknown"
}
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
formatter.timeZone = TimeZone(abbreviation: "UTC")
return formatter.string(from: date)
}
回答by Pedro
If you redefine __DATE__
and __TIME__
it will make the time update every time you build your app. you won′t need to clean or archive to update the time, just need run the project.
如果您重新定义__DATE__
,__TIME__
它会在您每次构建应用程序时更新时间。你不需要清理或存档来更新时间,只需要运行项目。
#define DATE [NSString stringWithUTF8String:__DATE__]
#define TIME [NSString stringWithUTF8String:__TIME__]
- (NSString *)getBuildDate {
NSString *buildDate;
// Get build date and time, format to 'yyMMddHHmm'
NSString *dateStr = [NSString stringWithFormat:@"%@ %@", DATE , TIME ];
// Convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"LLL d yyyy HH:mm:ss"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormat setLocale:usLocale];
NSDate *date = [dateFormat dateFromString:dateStr];
// Set output format and convert to string
[dateFormat setDateFormat:@"dd/MM/yyyy-HH:mm"];
buildDate = [dateFormat stringFromDate:date];
return buildDate;
}
回答by Arunabh Das
Swift5
斯威夫特5
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)