Myx*_*tic 15 xcode build objective-c ios
我能够在模拟器中显示我的应用程序的构建日期,但每当我存档应用程序并将其上传到TestFlight,然后将其安装在设备上时,构建日期都不会显示.
这是我正在做的显示构建日期.
首先,我将CFBuildDate作为字符串添加到myproject-info.plist中
接下来,我将以下脚本添加到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
Run Code Online (Sandbox Code Playgroud)
最后,使用以下代码从plist文件中获取构建日期:
NSString *build_date = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBuildDate"];
Run Code Online (Sandbox Code Playgroud)
这会在模拟器中显示构建日期(虽然偶尔也没有),但是当通过TestFlight部署应用程序时,构建日期永远不会显示.有任何想法吗 ?
提前致谢.
Jer*_*emy 27
您可以考虑使用内置__DATE__
和__TIME__
宏,它们将返回构建应用程序的日期和时间的字符串表示形式.也许他们会对你有更多的帮助:
NSString *dateStr = [NSString stringWithUTF8String:__DATE__];
NSString *timeStr = [NSString stringWithUTF8String:__TIME__];
Run Code Online (Sandbox Code Playgroud)
小智 12
要获得格式为'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;
}
Run Code Online (Sandbox Code Playgroud)
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)
}
Run Code Online (Sandbox Code Playgroud)
如果您重新定义__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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6903 次 |
最近记录: |